Controlling actions
Create site6 by copying site5.
- /cms
- ...
- site5
- site6
In this chapter, we are going to program how a URL is interpreted and converted into an action.
To test the result online, enter http://www.frasq.org/cms/site6 in the address bar of your navigator.
Create the folder actions in site6.
- /cms/site6
- actions
Add the file home.php in the folder actions with the following content:
- /cms/site6
- actions
- home.php
- actions
- function home($lang) {
- head('title', translate('home:title', $lang));
- $footer = view('footer', $lang);
- $content = view('home', $lang);
- $output = layout('standard', compact('footer', 'content'));
- return $output;
- }
The function home
has one argument, $lang
, the display language of the content.
home
starts by adding the title of the page to the head section of the document then generates the views for the central part and footer of the page.
The whole thing is assembled with the layout
function.
home
returns the body of the document.
Add the title of the home page in strings.inc.
- global $strings;
- $strings = array(
- array('title' => 'frasq.org',
- ),
- 'fr' => array(
- 'description' => 'frasq.org : Apprenez comment écrire votre propre CMS en PHP.',
- 'keywords' => 'PHP HTML CSS CMS MVC site web language programmation tutoriel manuel cours exemple',
- 'home:title' => 'Accueil',
- ),
- 'en' => array(
- 'description' => 'frasq.org : Learn how to write your own CMS in PHP.',
- 'keywords' => 'PHP HTML CSS CMS MVC web site langage programming tutorial manual course example',
- 'home:title' => 'Home',
- ),
- );
description
and keywords
define the default contents of the tags meta name="description"
and meta name="keywords"
, essential to search engines.
Add the file aliases.inc in the folder includes with the following content:
- /cms/site6
- includes
- aliases.inc
- includes
- global $aliases;
- $aliases = array(
- 'fr' => array(
- 'accueil' => 'home',
- ),
- 'en' => array(
- 'home' => 'home',
- ),
- );
aliases.inc defines the global array $aliases
which associates, depending on a language, a path in a URL to an action.
All the work of reading or writing URLs is realized in engine.php.
- global $aliases;
- $aliases = array();
- @include 'aliases.inc';
Loads the global array $aliases
.
- define('ACTIONS_DIR', ROOT_DIR . DIRECTORY_SEPARATOR . 'actions');
Defines where all actions are stored. An action is a function defined in a file with the same name.
- function url($action, $lang=false, $args=false) {
- global $base_path;
- return $base_path.'/'.alias($action, $lang, $args);
- }
url
returns the URL of an action, depending on a language.
- function alias($action, $lang=false, $args=false) {
- $path = detour($action, $lang);
- if ($args) {
- if ($path) {
- $path .= '/';
- }
- $path .= implode('/', $args);
- }
- return $lang ? $lang.'/'.$path : $path;
- }
alias
returns the path in URL for an action, depending on a language.
- function detour($action, $lang=false) {
- global $aliases;
- if ($lang && array_key_exists($lang, $aliases)) {
- if (($path = array_search($action, $aliases[$lang]))) {
- return $path;
- }
- }
- if (array_key_exists(0, $aliases)) {
- if (($path = array_search($action, $aliases[0]))) {
- return $path;
- }
- }
- return false;
- }
detour
returns the alias of an action, depending on a language.
- function route($query, $lang=false) {
- global $aliases;
- $args = array();
- if (empty($query)) {
- return array('home', false);
- }
- $s = explode('/', $query);
- while (count($s) > 0) {
- $p = implode('/', $s);
- if ($lang && array_key_exists($lang, $aliases) && array_key_exists($p, $aliases[$lang])) {
- return array($aliases[$lang][$p], $args);
- }
- if (array_key_exists(0, $aliases) && array_key_exists($p, $aliases[0])) {
- return array($aliases[0][$p], $args);
- }
- array_unshift($args, array_pop($s));
- }
- return false;
- }
route
returns the action associated to a path in a URL, depending on a language.
The rest of the path is also returned as a list of arguments.
If the path is empty, route
refers to the home page.
If the path isn't matched with an action, route
returns false
.
- $action=$args=false;
- $r = route($path, $lang);
- if (!$r) {
- header('HTTP/1.0 404 Not Found');
- exit;
- }
- else {
- list($action, $args) = $r;
- }
- run($action, $lang, $args);
- }
The end of dispatch
now calls route
to obtain the name of an action and a list of arguments and passes them with the display language to run
.
If no action is associated to the URL, an HTTP 404 error is sent back to the requester.
- function run($action, $lang=false, $args=false) {
- head('lang', $lang);
- head('title', translate('title', $lang));
- head('description', translate('description', $lang));
- head('keywords', translate('keywords', $lang));
- head('favicon', 'favicon');
- $file = ACTIONS_DIR.DIRECTORY_SEPARATOR.$action.'.php';
- require_once $file;
- $func = basename($action);
- if ($lang) {
- if ($args) {
- array_unshift($args, $lang);
- }
- else {
- $args = array($lang);
- }
- }
- $output = $args ? call_user_func_array($func, $args) : call_user_func($func);
- if ($output) {
- echo $output;
- }
- exit;
- }
run
begins by filling some meta
tags of the head section of the document with their default values.
The rest of the code loads the file corresponding the requested action then calls the function with the same name with the language and the arguments of the action as parameters.
run
sends back all the generated document and terminates the execution of the request.
Enter http://localhost/cms/site6 in the address bar of your navigator.
Display the source code of the document. Check the result, in particular the head
section.
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" lang="en" />
- <meta name="description" content="frasq.org : Learn how to write your own CMS in PHP." />
- <meta name="keywords" content="PHP HTML CSS CMS MVC web site langage programming tutorial manual course example" />
- <meta name="robots" content="index, follow" />
- <link href="/cms/site6/css/zero.css" rel="stylesheet" type="text/css" media="screen" />
- <link href="/cms/site6/css/screen.css" rel="stylesheet" type="text/css" media="screen" />
- <link href="/cms/site6/css/theme.css" rel="stylesheet" type="text/css" media="screen" />
- <link href="/cms/site6/css/print.css" rel="stylesheet" type="text/css" media="print" />
- <link rel="shortcut icon" href="/cms/site6/favicon.ico" type="image/x-icon" />
- <title>Home</title>
- </head>
- <body>
- <div id="content">
- <h3>Welcome</h3>
- </div>
- <div id="footer">
- <p>©2010 frasq.org - All rights reserved - <a href="http://www.frasq.org">www.frasq.org</a> - May 13th, 2010</p>
- <p><img src="/cms/site6/images/ubuntu.png" alt="" width="16" height="16"/> <a href="http://www.ubuntu.com" target="_blank">Ubuntu</a></p>
- </div>
- </body>
- </html>
Comments