Creating a 'Page Controller' site
From Photon Framework
Contents |
Basic Setup
Start out with the most basic directory structure required:
myapp/
classes/
photon/
(photon code)
docroot/
index.php
pages/
Even though this will be a Page Controller site, we still need an index page that will bootstrap our application:
/myapp/docroot/index.php <source lang="php"> <?php // create a constant to refer to the root of the application // and add the classes dir to the include path for convenience define('APPROOT', dirname(__FILE__).'/..'); set_include_path(APPROOT.'/classes:'.get_include_path());
// Figure out which page the browser is requesting. // For this example we will assume that the page will be anything after /index.php // so if the url is /index.php/admin/manage then we will assume that the page being // requested is /admin/manage // We can later change this to use mod_rewrite and make the urls pretty $req = substr($_SERVER['REQUEST_URI'], strlen('/index.php'));
// let's create start the application // and tell it the path to the pages require 'photon/web/PageControllerApp.php'; require 'photon/web/PhpViewRenderer.php';
$app = new PageControllerApp(array(
'pagesDir' => APPROOT.'/pages',
'baseUrl' => '/index.php',
'viewRenderer' => new PhpViewRenderer(array(
'templateDir' => APPROOT.'/pages'
))
));
// now let's tell it to process the request for the page
$app->run();
</source>
Now let's create a page, and a template for the page:
/myapp/pages/index.php <source lang="php"> <?php class IndexPage extends Page {
function init () {
$this->title = 'MyApp - Main Page';
}
} </source>
/myapp/pages/index.phtml <source lang="php"> <html> <head>
<title><?=$this->title></title>
</head> <body>
Welcome
Hello World
</body> </html> </source>
Tags
Since the tags and the PhotonApp are completely decoupled, we need to make some changes to our bootstrap file to process the tags:
<source lang="php" strict="false"> /myapp/docroot/index.php <?php ... require 'photon/ui/PhotonTagProcessor.php';
$handler = new HandlerChain(
new PhotonTagProcessor(APPROOT.'/tags'), new PhotonApp(APPROOT.'/pages')
);
$handler->process( $req ); </source>
Since we don't want to duplicate a lot the same code on every page, we will create a 'Site' tag that all pages can reference:
<source lang="php" strict="false">
/myapp/tags/Site.phtml
<html>
<head>
<title><?=$title></title>
</head> <body>
<?=$content?>
</body> </head> </source>
So, now we can update our index.phtml template to use the Site tag:
<source lang="php" strict="false"> /myapp/pages/index.phtml <p:Site title='<?=$title?>'>
Welcome
Hello World
</p:Site> </source>
Actions
Let's say this page allows a user to add a project. We first need to create a form:
<source lang="php" strict="false"> /myapp/pages/index.phtml <p:Site title="<?=$title?>">
Welcome
Create a Project:
<form method='POST'>
<input type='hidden' name='action' value='addProject' /> Project Name: <input name='Name' /> <button type='submit'>Add Project</button>
</form>
</p:Site> </source>
The hidden input field called 'action' is what determines which method gets called on the IndexPage class:
<source lang="php"> /myapp/pages/index.php <?php class IndexPage extends Page {
function addProject () {
$project = new Project;
$project->Name = $_POST['Name'];
// perform the insert (or action) MyApp::save($project);
// redirect to the form
// always do this in order to prevent
// the "The page cannot refresh without resending the information" error
$this->redirect("/");
}
function render () {
$title = 'MyApp - Main Page';
require 'index.phtml';
}
} </source>
