Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Lithium continuation route examples
<?php
/**
* Continuation routing examples. Handles URLs in the following forms:
*
* /posts
* /en/posts
* /admin/posts
* /admin/en/posts
* /admin/en/posts.json
* /admin/en/posts/4ef16ccc7f8b9aa331000064.json
* /admin/posts/4ef16ccc7f8b9aa331000064.json
* /en/posts/edit/4ef16ccc7f8b9aa331000064
* /posts/4ef16ccc7f8b9aa331000064
*
* ...as well as many, many other permutations.
*/
use lithium\net\http\Router;
use lithium\core\Environment;
use lithium\action\Dispatcher;
/**
* Add a Dispatcher rule to rewrite actions with prefixes whenever the `admin` flag is encountered
* in parameters returned from routing.
*/
Dispatcher::config(['rules' => [
'admin' => ['action' => 'admin_{:action}']
]]);
/**
* Continuation route for admin requests. Instead of dispatching to `{Controller}::{action}()`,
* matching routes will dispatch to `{Controller}::admin_{action}()`, per the dispatch rules
* defined above.
*/
Router::connect('/admin/{:args}', ['admin' => true], [
'continue' => true, 'persist' => ['controller', 'admin']
]);
/**
* Continuation routes for locale detection. Make sure to include `bootstrap/g11n.php`
* and configure your locales.
*/
Router::connect(
'/{:locale:' . join('|', array_keys(Environment::get('locales'))) . '}/{:args}',
[],
['continue' => true]
);
/**
* Static pages.
*/
Router::connect('/', 'Pages::view');
Router::connect('/pages/{:args}', 'Pages::view');
/**
* Handling API output formats.
*/
Router::connect('/{:args}.{:type:json|rss|xml}', [], ['continue' => true]);
/**
* MongoDB document routes.
*/
Router::connect('/{:controller}/{:id:[0-9a-f]{24}}', ['action' => 'view']);
Router::connect('/{:controller}/{:action}/{:id:[0-9a-f]{24}}');
/**
* Default route.
*/
Router::connect('/{:controller}/{:action}/{:args}');
?>

ppadron commented Dec 23, 2011

Nice example! Is it possible to make admin routes to be handled by separate controllers under a app\controllers\admin namespace?

gwoo commented Dec 23, 2011

instead of setting action, try setting "controller".

Dispatcher::config(array('rules' => array(
    'admin' => array('controller' => 'admin\{:controller}')
)));

mehlah commented Jan 15, 2012

You have to give it the full namespace

Dispatcher::config(array('rules' => array(
    'admin' => array('controller' => 'app\controllers\admin\{:controller}Controller')
)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment