Skip to content

Commit

Permalink
Passing $route to the after($output, $route) abstract function. Addin…
Browse files Browse the repository at this point in the history
…g documentation for before($route) and after($output, $route) functions and updating examples.
  • Loading branch information
Fabrice Luraine committed Feb 21, 2010
1 parent 744a4da commit 3a6534a
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 12 deletions.
4 changes: 2 additions & 2 deletions LISEZMOI.mkd
Expand Up @@ -285,15 +285,15 @@ La sortie est temporisée afin de prendre en charge aisément des fichiers de gr

Vous pouvez définir une fonction `before` qui sera executée avant chaque requête. Cela s'avère très utile pour définir un layout par défaut ou des variables à passer aux templates

function before()
function before($route)
{
layout('default_layout.php');
set('site_title', 'My Website');
}

Un filtre de sortie `after` est également disponible. Il est exécuté après chaque requête et permet d'appliquer une transformation à la sortie (sauf pour les sorties `render_file` qui sont envoyées directement au tampon de sortie).

function after($output){
function after($output, $route){
$config = array('indent' => TRUE,
'output-xhtml' => TRUE,
'wrap' => 200);
Expand Down
14 changes: 12 additions & 2 deletions README.mkd
Expand Up @@ -297,23 +297,33 @@ Output is temporized so that it can easily handle large files.

You can define a `before` function that will be executed before each request. This is very useful to define a default layout or passing common variables to the templates

function before()
function before($route)
{
layout('default_layout.php');
set('site_title', 'My Website');
}

The current matching route is also passed to the before function, so you can test it. It's an array as returned by the internal `route_find` function, with those values:

* `method` (HTTP method)
* `pattern` (regexp pattern)
* `names` (params names)
* `function` (callback)
* `options` (route options)
* `params` (current params)

An `after` output filter is also available. It's executed after each request and can apply a transformation to the output (except for `render_file` outputs which are sent directly to the output buffer).

function after($output){
function after($output, $route){
$config = array('indent' => TRUE,
'output-xhtml' => TRUE,
'wrap' => 200);

$tidy = tidy_parse_string($output, $config, option('encoding'));
return $tidy->cleanRepair();
}

The current executed route is also available for `after` function.

## Configuration ##

Expand Down
10 changes: 7 additions & 3 deletions examples/example01/index.php
Expand Up @@ -6,8 +6,9 @@ function configure()
option('env', ENV_DEVELOPMENT);
}

function before()
function before($route)
{
header("X-LIM-route-function: ".$route['function']);
layout('html_my_layout');
}

Expand Down Expand Up @@ -81,10 +82,13 @@ function image_show_jpeg_only()
render_file($filename);
}

function after($output)
function after($output, $route)
{
$time = number_format( (float)substr(microtime(), 0, 10) - LIM_START_MICROTIME, 6);
$output .= "<!-- page rendered in $time sec., on ".date(DATE_RFC822)."-->";
$output .= "\n<!-- page rendered in $time sec., on ".date(DATE_RFC822)." -->\n";
$output .= "<!-- for route\n";
$output .= print_r($route, true);
$output .= "-->";
return $output;
}

Expand Down
5 changes: 4 additions & 1 deletion examples/example02/index.php
Expand Up @@ -10,8 +10,11 @@ function configure()
option('debug', true);
}

function before()
function before($route)
{
header("X-LIM-route-function: ".$route['function']);
header("X-LIM-route-params: ".json_encode($route['params']));
header("X-LIM-route-options: ".json_encode($route['options']));
layout('html_my_layout'); # setting default html layout
//error_layout('html_my_layout');
}
Expand Down
10 changes: 10 additions & 0 deletions examples/example03/index.php
Expand Up @@ -18,6 +18,16 @@ function before($route = array())

}

function after($output, $route)
{
$time = number_format( (float)substr(microtime(), 0, 10) - LIM_START_MICROTIME, 6);
$output .= "\n<!-- page rendered in $time sec., on ".date(DATE_RFC822)." -->\n";
$output .= "<!-- for route\n";
$output .= print_r($route, true);
$output .= "-->";
return $output;
}

# defaults work the same as always...
dispatch('/', 'hello_world');
function hello_world()
Expand Down
1 change: 1 addition & 0 deletions examples/index.php
Expand Up @@ -14,6 +14,7 @@
<ul>
<li><a href="example01/">example 01: Hello World!</a></li>
<li><a href="example02/">example 02: errors</a></li>
<li><a href="example03/">example 03: routes with options, and conditional before function</a></li>
<li><a href="urlrewrite/"> Url Rewriting with Limonade.</a></li>
</ul>
</body>
Expand Down
6 changes: 4 additions & 2 deletions lib/limonade.php
Expand Up @@ -407,7 +407,7 @@ function autoload_controller($callback)
# 6.4 Call matching controller function and output result
if($output = call_user_func_array($route['function'], array_values($route['params'])))
{
echo after(error_notices_render() . $output);
echo after(error_notices_render() . $output, $route);
}
}
else halt(SERVER_ERROR, "Routing error: undefined function '{$route['function']}'", $route);
Expand Down Expand Up @@ -1272,7 +1272,9 @@ function route_build($method, $path_or_array, $func, $options = array())
* @access private
* @param string $method
* @param string $path
* @return array,false
* @return array,false route array has same keys as route returned by
* {@link route_build()} ("method", "pattern", "names", "function", "options")
* + the processed "params" key
*/
function route_find($method, $path)
{
Expand Down
8 changes: 6 additions & 2 deletions lib/limonade/abstract.php
Expand Up @@ -46,9 +46,11 @@ function autoload_controller($callback)
* to the templates.
*
* @abstract this function might be redefined by user
* @param array() $route array (like returned by {@link route_build()},
* with keys "method", "pattern", "names", "function", "options")
* @return void
*/
function before()
function before($route)
{

}
Expand All @@ -61,9 +63,11 @@ function before()
*
* @abstract this function might be redefined by user
* @param string $output
* @param array() $route array (like returned by {@link route_find()},
* with keys "method", "pattern", "names", "function", "params", "options")
* @return string
*/
function after($output)
function after($output, $route)
{
# Call functions...
# .. modifies $output...
Expand Down

0 comments on commit 3a6534a

Please sign in to comment.