Routing - darkredz/DooJ GitHub Wiki
#Routing Create your routes in protected/config/routes.conf.php
Format
$route[Request Method][Uri] = array(Controller, action method, other options)
##Basics http://localhost/ matches
$route['*']['/'] = ['MainController', 'index'];
http://localhost/hello matches
$route['*']['/hello'] = ['MainController', 'hello'];
http://localhost/another/example matches
$route['*']['/another/example'] = ['MainController', 'getExample'];
##RESTful routes 4 types of request methods are supported, GET, POST, PUT, DELETE. If you want a route to be accessed in any of the request methods, use *
// Can be access by any request methods
$route['*']['/api/list'] = array('RestController', 'listFood');
// Post request only
$route['post']['/api/create'] = array('RestController', 'createFood');
// Put request only
$route['put']['/api/update'] = array('RestController', 'updateFood');
// Delete request only
$route['delete']['/api/delete'] = array('RestController', 'deleteFood');
##Parameters in route
http://localhost/news/1234 routes to getNews() method. Get the parameter with $this->params['id']
$route['*']['/news/:id'] = array('NewsController', 'getNews');
//controller class
class NewsController extends DooController {
public function getNews(){
echo $this->params['id'];
}
}
##Extension name Some times it is great to add extension at the end of a URL (great for REST api). If you need to do so, simply add the extension name in your routes:
$route['*']['/simple.rss'] = ['FeedController', 'getRss'];
$route['*']['/simple.atom'] = ['FeedController', 'getAtom'];
It is a bit different if you want to have add it to a route with parameters:
$route['*']['/news/list/:id'] = ['FeedController',
'listNews',
'extension'=>'.json'];
//Or multiple extension names.
$route['*']['/news/list/:id'] = ['FeedController',
'listNews',
'extension' => ['.json', '.xml'] ];
Access it at http://localhost/news/list/168.json OR 168.xml
##Redirections Here's how you do redirection to an existing route internally
$route['*']['/simple'] = ['MainController', 'soSimple'];
//Http status code is optional, default is 302 Moved Temporarily
$route['*']['/easy'] = array('redirect', '/simple');
$route['*']['/easier'] = array('redirect', '/simple', 301);
When you access about or home, you will be redirected to the main page. When you access easy or easier, you will be redirected to simple.
To redirect a local URL to another website just pass in the full URL (http://)
// External redirect
$route['*']['/doophp'] = array('redirect', 'http://doophp.com/');
##Matching parameters
If you wanted to filter the parameter values in the route(instead of doing it in Controller), you can use Regular expressions to do some matching.
Below is an identical routes matching examples. You must assigned a matching pattern against parameter values. If no pattern is assigned, it will match the first route defined.
$route['*']['/news/:id'] = ['NewsController', 'showNewsById',
'match' => [
'id' => '/^\d+$/'
]
];
$route['*']['/news/:title'] = ['NewsController', 'showNewsByTitle',
'match' => [
'title'=>'/[a-z0-9]+/'
]
];
See Next Auto Routing