Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use explicitly 'callback' pseudo-type instead of 'function'.

In route() functions, replace "function" variables and named keys name with "callback" (example: in route_build() returned array).
Update tests.
Update documentation and enhanced it with controllers callbacks explanations.
  • Loading branch information...
commit 269903dde288a8c138fbe029fd6a4595b5e6b9c7 1 parent 009d98f
Fabrice Luraine authored
View
43 README.mkd
@@ -30,13 +30,20 @@ A [discussion group](http://groups.google.fr/group/limonade) is also available f
## Routes ##
-Routes combine an HTTP method with an URL matching pattern and a function
+Routes combine
+
+* an HTTP method
+* with an URL matching pattern
+* and a callback parameter
+
+So they make the glue between an URL + a HTTP method, and the code provided in a callback controller.
dispatch('/', 'my_get_function');
# same as dispatch_get('my_get_function');
function my_get_function()
{
// Show something
+ // with the code of this callback controller
}
dispatch_post('/', 'my_post_function');
@@ -74,6 +81,8 @@ When `PUT` or `DELETE` methods are not supported (like in HTML form submision),
<p><input type="submit" value="Update"></p>
</form>
+### Routing patterns ###
+
Patterns may include named parameters. Associated values of those parameters are available with the `params()` function.
dispatch('/hello/:name', 'hello');
@@ -131,7 +140,31 @@ Wildcard parameters and regular expressions may be named too.
$name = params('name');
}
-Functions called by routes can be written anywhere before the execution of the `run()` function. They can also be grouped in controllers files stored in a `controllers/` folder.
+### Callback controllers ###
+
+The callback can be a function, an object method, a static method or a closure.
+See [php documentation](http://php.net/manual/en/language.pseudo-types.php#language.types.callback) to learn more about the callback pseudo-type.
+
+ # will call my_hello_function() function
+ dispatch('/hello', 'my_hello_function');
+
+ # Static class method call, MyClass::hello();
+ dispatch('/hello', array('MyClass', 'hello'));
+
+ # Object method call, $obj->hello();
+ dispatch('/hello', array($obj, 'hello'));
+
+ # Static class method call (As of PHP 5.2.3), MyClass::hello();
+ dispatch('/hello', 'MyClass::hello');
+
+ # Using lambda function (As of PHP 5.3.0)
+ dispatch('/hello', function(){
+ return 'Hello World!';
+ });
+
+Callback controllers return the rendered view output (see _Views and templates_).
+
+Callbacks called by routes can be written anywhere before the execution of the `run()` function. They can also be grouped in controllers files stored in a `controllers/` folder.
/ # site root
- index.php # file with routes declarations and run()
@@ -245,7 +278,7 @@ If layout value is `null`, rendering will be done without any layout.
Formatted string can be used like with [`sprintf`](http://php.net/manual/function.sprintf.php):
set('num', 5);
- set('tree');
+ set('where', 'tree');
return render('There are %d monkeys in the %s') // returns 'There are 5 monkeys in the tree'
It's also possible to provide a function name as a template. By this way, we can for example produce a single file application.
@@ -336,7 +369,7 @@ The current matching route is also passed to the before function, so you can tes
* `method` (HTTP method)
* `pattern` (regexp pattern)
* `names` (params names)
-* `function` (callback)
+* `callback` (callback)
* `options` (route options)
* `params` (current params)
@@ -371,7 +404,7 @@ You can define your own `autorender` function to make automatic rendering depen
function autorender($route)
{
- $view = $route['function'] . ".html.php";
+ $view = $route['callback'] . ".html.php";
return html($view);
}
View
3  TODO
@@ -6,7 +6,6 @@
### 0.5 ###
- complete CHANGES file
-- in route() functions, replace "function" name with "callback" name (example: in route_build() returned array). Enhanced use of callback pseudo-type instead of functions names.
- testing calling lambdas from dispatch functions, with native php callback (see <http://limonade.tumblr.com/post/247098505/hidden-features>).
- Adding documentation about dispatch callback (see blog post <http://limonade.tumblr.com/post/247098505/hidden-features>) (remember that controller function args must be in the same order as route params!)
- explain controllers return output in README
@@ -57,6 +56,8 @@
- in debug output, formating debug_backtrace to be me readable (like debug_backtrace output but with extra informations that can be toggled). Using show_settings view by kematzy ? http://github.com/kematzy/limonade/commit/02ad17260912757b73ff809acad8e970efafc4b4
- improve security in render_file with a safe_dir option
- new redirect_to (support for https)
+- Use callback pseudo-type instead of function in error user defined handling.
+
### 0.7 ###
View
56 lib/limonade.php
@@ -411,19 +411,19 @@ function autoload_controller($callback)
require_once_dir(option('controllers_dir'));
}
}
- autoload_controller($route['function']);
+ autoload_controller($route['callback']);
- if(is_callable($route['function']))
+ if(is_callable($route['callback']))
{
# 6.3 Call before function
call_if_exists('before', $route);
# 6.4 Call matching controller function and output result
- $output = call_user_func_array($route['function'], array_values($route['params']));
+ $output = call_user_func_array($route['callback'], array_values($route['params']));
if(is_null($output)) $output = call_if_exists('autorender', $route);
echo after(error_notices_render() . $output, $route);
}
- else halt(SERVER_ERROR, "Routing error: undefined function '{$route['function']}'", $route);
+ else halt(SERVER_ERROR, "Routing error: undefined function '{$route['callback']}'", $route);
}
else route_missing($rm, request_uri($env));
@@ -1088,62 +1088,62 @@ function request_uri($env = null)
*
* @return void
*/
-function dispatch($path_or_array, $function, $options = array())
+function dispatch($path_or_array, $callback, $options = array())
{
- dispatch_get($path_or_array, $function, $options);
+ dispatch_get($path_or_array, $callback, $options);
}
/**
* Add a GET route. Also automatically defines a HEAD route.
*
* @param string $path_or_array
- * @param string $function
+ * @param string $callback
* @param array $options (optional). See {@link route()} for available options.
* @return void
*/
-function dispatch_get($path_or_array, $function, $options = array())
+function dispatch_get($path_or_array, $callback, $options = array())
{
- route("GET", $path_or_array, $function, $options);
- route("HEAD", $path_or_array, $function, $options);
+ route("GET", $path_or_array, $callback, $options);
+ route("HEAD", $path_or_array, $callback, $options);
}
/**
* Add a POST route
*
* @param string $path_or_array
- * @param string $function
+ * @param string $callback
* @param array $options (optional). See {@link route()} for available options.
* @return void
*/
-function dispatch_post($path_or_array, $function, $options = array())
+function dispatch_post($path_or_array, $callback, $options = array())
{
- route("POST", $path_or_array, $function, $options);
+ route("POST", $path_or_array, $callback, $options);
}
/**
* Add a PUT route
*
* @param string $path_or_array
- * @param string $function
+ * @param string $callback
* @param array $options (optional). See {@link route()} for available options.
* @return void
*/
-function dispatch_put($path_or_array, $function, $options = array())
+function dispatch_put($path_or_array, $callback, $options = array())
{
- route("PUT", $path_or_array, $function, $options);
+ route("PUT", $path_or_array, $callback, $options);
}
/**
* Add a DELETE route
*
* @param string $path_or_array
- * @param string $function
+ * @param string $callback
* @param array $options (optional). See {@link route()} for available options.
* @return void
*/
-function dispatch_delete($path_or_array, $function, $options = array())
+function dispatch_delete($path_or_array, $callback, $options = array())
{
- route("DELETE", $path_or_array, $function, $options);
+ route("DELETE", $path_or_array, $callback, $options);
}
@@ -1199,16 +1199,16 @@ function route_reset()
* @access private
* @param string $method allowed http method (one of those returned by {@link request_methods()})
* @param string|array $path_or_array
- * @param callback $func callback function called when route is found. It can be
+ * @param callback $callback callback called when route is found. It can be
* a function, an object method, a static method or a closure.
* See {@link http://php.net/manual/en/language.pseudo-types.php#language.types.callback php documentation}
* to learn more about callbacks.
* @param array $options (optional). Available options:
* - 'params' key with an array of parameters: for parametrized routes.
* those parameters will be merged with routes parameters.
- * @return array array with keys "method", "pattern", "names", "function", "options"
+ * @return array array with keys "method", "pattern", "names", "callback", "options"
*/
-function route_build($method, $path_or_array, $func, $options = array())
+function route_build($method, $path_or_array, $callback, $options = array())
{
$method = strtoupper($method);
if(!in_array($method, request_methods()))
@@ -1299,7 +1299,7 @@ function route_build($method, $path_or_array, $func, $options = array())
return array( "method" => $method,
"pattern" => $pattern,
"names" => $names,
- "function" => $func,
+ "callback" => $callback,
"options" => $options );
}
@@ -1312,7 +1312,7 @@ function route_build($method, $path_or_array, $func, $options = array())
* @param string $method
* @param string $path
* @return array,false route array has same keys as route returned by
- * {@link route_build()} ("method", "pattern", "names", "function", "options")
+ * {@link route_build()} ("method", "pattern", "names", "callback", "options")
* + the processed "params" key
*/
function route_find($method, $path)
@@ -1821,18 +1821,18 @@ function benchmark()
/**
* Calls a function if exists
*
- * @param callback $func a function stored in a string variable,
+ * @param callback $callback a function stored in a string variable,
* or an object and the name of a method within the object
* See {@link http://php.net/manual/en/language.pseudo-types.php#language.types.callback php documentation}
* to learn more about callbacks.
* @param mixed $arg,.. (optional)
* @return mixed
*/
-function call_if_exists($func)
+function call_if_exists($callback)
{
$args = func_get_args();
- $func = array_shift($args);
- if(is_callable($func)) return call_user_func_array($func, $args);
+ $callback = array_shift($args);
+ if(is_callable($callback)) return call_user_func_array($callback, $args);
return;
}
View
8 lib/limonade/abstract.php
@@ -44,7 +44,7 @@ function initialize()
* </code>
*
*
- * @param string $callback the callback deined in matching route
+ * @param string $callback the callback defined in matching route
* @return void
*/
function autoload_controller($callback)
@@ -59,7 +59,7 @@ function autoload_controller($callback)
*
* @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")
+ * with keys "method", "pattern", "names", "callback", "options")
* @return void
*/
function before($route)
@@ -76,7 +76,7 @@ function before($route)
* @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")
+ * with keys "method", "pattern", "names", "callback", "params", "options")
* @return string
*/
function after($output, $route)
@@ -166,7 +166,7 @@ function before_render($content_or_func, $layout, $locals, $view_path)
*
* @abstract this function might be defined by user
* @param array() $route array (like returned by {@link route_build()},
- * with keys "method", "pattern", "names", "function", "options")
+ * with keys "method", "pattern", "names", "callback", "options")
* @return string
*/
function autorender($route)
View
2  tests/apps/02-outputs.php
@@ -33,7 +33,7 @@ function empty_controller()
}
function autorender($route){
- return "AUTORENDERED OUTPUT for ".$route['function'];
+ return "AUTORENDERED OUTPUT for ".$route['callback'];
}
run();
View
38 tests/router.php
@@ -17,7 +17,7 @@ function test_router_build_route()
assert_equal($r["method"], "GET");
assert_equal($r["pattern"], "#^/index(?:/*?)?$#i");
assert_empty($r["names"]);
- assert_equal($r["function"], "get_index");
+ assert_equal($r["callback"], "get_index");
/* testing very simple route with no parameters */
assert_match($r["pattern"], "/index");
@@ -158,7 +158,7 @@ function test_router_route()
assert_equal($r[0]["method"], "GET");
assert_equal($r[0]["pattern"], "#^/index(?:/*?)?$#i");
assert_empty($r[0]["names"]);
- assert_equal($r[0]["function"], "my_func");
+ assert_equal($r[0]["callback"], "my_func");
assert_empty($r[0]["options"]);
$r = route("put", "/blog/:id", "my_update_func");
@@ -168,7 +168,7 @@ function test_router_route()
assert_match($r[1]["pattern"], "/blog/102");
assert_length_of($r[1]["names"], 1);
assert_equal($r[1]["names"][0], "id");
- assert_equal($r[1]["function"], "my_update_func");
+ assert_equal($r[1]["callback"], "my_update_func");
assert_empty($r[1]["options"]);
$r = route("post", "/blog/:id", "my_post_func", array('params' => array('extra' => 10)));
@@ -177,7 +177,7 @@ function test_router_route()
assert_match($r[2]["pattern"], "/blog/102");
assert_length_of($r[2]["names"], 1);
assert_equal($r[2]["names"][0], "id");
- assert_equal($r[2]["function"], "my_post_func");
+ assert_equal($r[2]["callback"], "my_post_func");
assert_not_empty($r[2]["options"]);
assert_not_empty($r[2]["options"]['params']);
assert_equal($r[2]["options"]['params']['extra'], 10);
@@ -206,18 +206,18 @@ function test_router_find_route()
assert_false($r);
$r = route_find("POST", "/create");
- assert_equal($r["function"], "my_create_func");
+ assert_equal($r["callback"], "my_create_func");
$r = route_find("GET", "/edit");
- assert_equal($r["function"], "my_edit_func");
+ assert_equal($r["callback"], "my_edit_func");
$r = route_find("GET", "/edit/120");
- assert_equal($r["function"], "my_edit_func");
+ assert_equal($r["callback"], "my_edit_func");
assert_equal($r["params"]["id"], 120);
$r = route_find("GET","/limonade.jpg/thumb", 'my_jpeg');
- assert_equal($r["function"], "my_jpeg");
+ assert_equal($r["callback"], "my_jpeg");
assert_equal($r["params"][0], "limonade");
assert_equal($r["params"]["size"], "thumb");
@@ -226,22 +226,22 @@ function test_router_find_route()
assert_length_of($routes, 10);
$r = route_find("GET", "/index");
- assert_equal($r["function"], "my_index_func");
+ assert_equal($r["callback"], "my_index_func");
$r = route_find("GET", "/index/ok");
- assert_equal($r["function"], "my_index_func2");
+ assert_equal($r["callback"], "my_index_func2");
$r = route_find("DELETE", "/delete");
- assert_equal($r["function"], "my_delete_func");
+ assert_equal($r["callback"], "my_delete_func");
$r = route_find("DELETE", "/delete/120");
- assert_equal($r["function"], "my_delete_func");
+ assert_equal($r["callback"], "my_delete_func");
$r = route_find("DELETE", "/delete/120/ok");
- assert_equal($r["function"], "my_delete_func2");
+ assert_equal($r["callback"], "my_delete_func2");
$r = route_find("GET", "/list/120");
- assert_equal($r["function"], "my_list_func");
+ assert_equal($r["callback"], "my_list_func");
/* testing parameterized functions */
$extra_p = array(123, 'id' => 123, 'name' => 'abc');
@@ -251,16 +251,16 @@ function test_router_find_route()
$routes = route( "get", "/indexed/cat/*", "my_p_func", array('params' => $extra_p));
$r = route_find("GET", "/no/cat/21");
- assert_equal($r["function"], "my_p_func");
+ assert_equal($r["callback"], "my_p_func");
assert_equal($r["params"]["id"], 21);
$r = route_find("GET", "/with/cat/21");
- assert_equal($r["function"], "my_p_func");
+ assert_equal($r["callback"], "my_p_func");
assert_equal($r["params"]["id"], 21);
assert_equal($r["params"]["name"], "abc");
$r = route_find("GET", "/indexed/cat/21");
- assert_equal($r["function"], "my_p_func");
+ assert_equal($r["callback"], "my_p_func");
assert_equal($r["params"][0], 21);
assert_equal($r["params"]["id"], 123);
assert_equal($r["params"]["name"], "abc");
@@ -268,11 +268,11 @@ function test_router_find_route()
/* testing route with special characters */
route( "get", "/mañana/:when", "my_special_func");
$r = route_find("GET", "/mañana/123");
- assert_equal($r["function"], "my_special_func");
+ assert_equal($r["callback"], "my_special_func");
assert_equal($r["params"]["when"], 123);
$r = route_find("GET", "/mañana/après demain");
- assert_equal($r["function"], "my_special_func");
+ assert_equal($r["callback"], "my_special_func");
assert_equal($r["params"]["when"], "après demain");
Please sign in to comment.
Something went wrong with that request. Please try again.