Last active
June 16, 2017 13:05
-
-
Save nateabele/1512502 to your computer and use it in GitHub Desktop.
Lithium continuation route examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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}'); | |
?> |
instead of setting action, try setting "controller".
Dispatcher::config(array('rules' => array(
'admin' => array('controller' => 'admin\{:controller}')
)));
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
Nice example! Is it possible to make admin routes to be handled by separate controllers under a app\controllers\admin namespace?