<?php
/**
* Zend Framework Examples
*
* @category ZfEx
* @package TryIt
* @author Michel Corne
* @copyright 2010 Michel Corne
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License
* @see http://zend-framework-examples.blogspot.com
*/
/*
In this example, we create routes and we compare URLs against those routes.
*/
/*
Loads the autoloader
*/
$zfVersion = '1.10.4'; // tested with this version
$zfDir = '../ZendFramework';
isset($_GET['zf-version']) and is_dir("$zfDir/{$_GET['zf-version']}") and
$zfVersion = $_GET['zf-version'];
set_include_path("$zfDir/$zfVersion/library");
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
/*
Using the router
*/
class MyRouter
{
const BASE_URL = 'http://domain/';
/* Description of the routes */
public static $routes = array(
0 => array('default'),
array(
'author/USER-NAME [controller="profile", action="userinfo"]',
'Zend_Controller_Router_Route',
'author/:username',
array(
'controller' => 'profile',
'action' => 'userinfo',
)
),
array(
'archive/YYYY [controller="archive", action="show", YEAR=2006]',
'Zend_Controller_Router_Route',
'archive/:year',
array(
'year' => 2006,
'controller' => 'archive',
'action' => 'show',
),
array('year' => '\d+')
),
array(
'case/NNN-DESCRIPTION.html [controller="case", action="view"]',
'Zend_Controller_Router_Route_Regex',
'case/(\d+)-(.+)\.html',
array(
'controller' => 'case',
'action' => 'view'
),
array(
1 => 'id',
2 => 'description'
)
),
);
/* Definition of the URLs */
public static $uri = array(
'default route' => array(
'',
'news',
'blog/archive',
'blog/archive/list',
'blog/archive/list/sort/alpha/date/desc',
'foo',
),
'author route' => array(
'author/martel',
'author/john/param/unexpected',
),
'archive route' => array(
'archive',
'archive/2008',
'archive/bad',
),
'case route' => array(
'case/123-the-good-case.html',
'case/xyz-the-bad-case.html',
),
);
/* Processing the routing request */
public function process()
{
// We get the route, the option to disable of the default routes,
// the option to ignore modules, the URL.
list($route, $disableDefault, $disableModule, $uri) = $this->_getParameters();
try {
// We create the front controller.
$front = Zend_Controller_Front::getInstance();
if (!$disableModule) {
// We ignore the modules if requested.
$front->addControllerDirectory('dummy', 'blog');
$front->addControllerDirectory('dummy', 'news');
}
// We instantiate the router.
$router = $front->getRouter();
// We remove the default routes if requested.
$disableDefault and $route and $router->removeDefaultRoutes();
// We add the route.
@list(, $class, $pattern, $defaults, $reqs) = self::$routes[$route];
$class and $router->addRoute('route',
new $class($pattern, $defaults, $reqs));
// We extract from the URL, the module, the controller, the action and the parameters.
$request = new Zend_Controller_Request_Http(self::BASE_URL . $uri);
$params = $router->route($request)->getParams();
$result = array_diff_key($params, $_GET);
} catch (Exception $e) {
// If we catch an exception, we return the error message.
$result = $e->getMessage();
}
return array($route, $disableDefault, $disableModule, $uri, $result);
}
/* Extraction of the parameters from the GET request */
private function _getParameters()
{
$route = (isset($_GET['route']) and isset(self::$routes[$_GET['route']]))?
$_GET['route'] : 0;
$disableDefault = empty($_GET)? 1 : !empty($_GET['disable-default']);
$disableModule = empty($_GET)? 0 : !empty($_GET['disable-module']);
$uri = isset($_GET['uri'])? $_GET['uri'] : '';
return array($route, $disableDefault, $disableModule, $uri);
}
}
/* Displaying items */
class MyHtml
{
/* Displaying the title of the page based on the file name. */
public static function printTitle()
{
$basename = basename(__FILE__, '.php');
$title = ucwords(str_replace('-' , ' ', $basename));
$zfVersion = Zend_Version::VERSION;
$phpVersion = phpversion();
echo "ZfEx $title (ZF/$zfVersion PHP/$phpVersion)";
}
/* Displaying a check mark */
public static function printChecked($value)
{
empty($value) or print 'checked="checked"';
}
/* Displaying the selected option */
public static function printSelected($value, $target)
{
$value == $target and print 'selected="selected"';
}
/* Colorization of data */
public static function colorize($mixed)
{
// We export the data as valid PHP code.
$mixed = is_object($mixed)? print_r($mixed, true) : var_export($mixed, true);
// We add the PHP tag, we colorize the code, we remove the PHP tag.
$mixed = '<?' . "php $mixed";
$mixed = str_replace("\'", 'ZFEX_QUOTE', $mixed);
$mixed = str_replace("'", 'ZFEX_QUOTE', $mixed);
$mixed = str_replace('"', 'ZFEX_DQUOTE', $mixed);
$mixed = highlight_string($mixed, true);
$mixed = str_replace('ZFEX_QUOTE', ''', $mixed);
$mixed = str_replace('ZFEX_DQUOTE', '"', $mixed);
$mixed = str_replace('<?php ', '', $mixed);
// We display the data.
echo "<pre>$mixed</pre>";
}
}
/*
Entry point
*/
// We process the routing request, and we get all the parameters, to display in the form.
$router = new MyRouter();
list($route, $disableDefault, $disableModule, $uri, $result) = $router->process();
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php MyHtml::printTitle();?></title>
<style type="text/css">
body, td {
font-family: arial, sans-serif;
font-size: 0.9em;
}
</style>
</head>
<body>
<p>EXAMPLE <?php MyHtml::printTitle();?></p>
<hr />
<form name="form">
<table>
<tr>
<td>Route</td>
<td>
<select name="route">
<?php foreach(MyRouter::$routes as $routeId => $details) :?>
<option value="<?php echo $routeId;?>"
<?php MyHtml::printSelected($route, $routeId);?>>
<?php echo $details[0];?>
</option>
<?php endforeach;?>
</select>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="checkbox" name="disable-default" value="1"
<?php MyHtml::printChecked($disableDefault);?> />
Remove default routes
</td>
<td>Ignored if default route selected</td>
</tr>
<tr>
<td></td>
<td>
<input type="checkbox" name="disable-module" value="1"
<?php MyHtml::printChecked($disableModule);?> />
Ignore modules (aka controller directory)
</td>
</tr>
<tr><td> </td></tr>
<tr>
<td>URI</td>
<td>
<select name="uri">
<?php foreach(MyRouter::$uri as $routeName => $uris) :?>
<optgroup label="<?php echo $routeName;?>">
<?php foreach($uris as $targetUri) :?>
<option value="<?php echo $targetUri;?>"
<?php MyHtml::printSelected($uri, $targetUri);?>>
<?php echo MyRouter::BASE_URL . $targetUri;?>
</option>
<?php endforeach;?>
</optgroup>
<?php endforeach;?>
</select>
</td>
</tr>
<tr><td> </td></tr>
<tr>
<td></td>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
<br />
<span style="font-size:75%">Change ZF Version</span>
<select style="font-size:75%" name="zf-version">
<?php foreach(array_map('basename', glob("$zfDir/*", GLOB_ONLYDIR)) as $dir) :?>
<option <?php MyHtml::printSelected($zfVersion, $dir);?>>
<?php echo $dir;?>
</option>
<?php endforeach;?>
</select>
</form>
<hr />
RESULT
<?php MyHtml::colorize($result);?>
</body>
</html>