Created
July 15, 2011 08:08
-
-
Save m4rw3r/1084295 to your computer and use it in GitHub Desktop.
Example router
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 | |
// Rules: | |
$this->root()->to('Test#index'); | |
$this->match('archive/:year/:month', array('year' => '\d{4}', 'month' => '1[0-2]|0[1-9]')) | |
->to('Blog#archive')->name('blog_archive'); | |
// Mount a callback (only string-callbacks are allowed, so no objects or closures yet) | |
$this->match('testing')->to('Sample\\SimpleApp::run')->name('simpleapp'); | |
// Redirect to other site, temporary | |
$this->match('about')->to($this->redirect('http\://www.google.com', 302)); | |
$this->match('search/:query')->to($this->redirect('http\://www.google.com/search?q=:query')); | |
$this->match('test')->to('test#lol'); | |
$r = $this->resources('test'); | |
// Mount a subapplication, the string parameter is a class inheriting from \Inject\Application\Engine | |
$this->match('admin*uri')->to('Admin\\Engine'); | |
// Default Route: | |
$this->match(':controller(/:action(/:id))(.:format)')->name('standard'); | |
// Result: | |
$router = function($env) use($engine, $controllers) | |
{ | |
$matches = array(); | |
if($env['PATH_INFO'] == '/') | |
{ | |
$env['web.route_params'] = $merged = array ( | |
'action' => 'index', | |
); | |
$ret = \Sample\Controller\Test::stack($engine, 'index')->run($env); | |
if(empty($ret[1]['X-Cascade']) OR $ret[1]['X-Cascade'] !== 'pass') | |
{ | |
return $ret; | |
} | |
} | |
if(stripos($env['PATH_INFO'], '/archive/') === 0) | |
{ | |
if(preg_match('#^/archive/(?<year>\\d{4})/(?<month>1[0-2]|0[1-9])$#ui', $env['PATH_INFO'], $match)) | |
{ | |
$matches[] = $match; | |
$env['web.route_params'] = $merged = array_intersect_key(array_merge(array ( | |
'action' => 'index', | |
), array_reduce($matches, 'array_merge', array())), array ( | |
'year' => 0, | |
'month' => 1, | |
)); | |
$ret = \Sample\Controller\Blog::stack($engine, 'archive')->run($env); | |
if(empty($ret[1]['X-Cascade']) OR $ret[1]['X-Cascade'] !== 'pass') | |
{ | |
return $ret; | |
} | |
array_pop($matches); | |
} | |
} | |
if(strtolower($env['PATH_INFO']) == '/testing') | |
{ | |
$env['web.route_params'] = $merged = array ( | |
); | |
$ret = call_user_func('Sample\\SimpleApp::run', $env); | |
if(empty($ret[1]['X-Cascade']) OR $ret[1]['X-Cascade'] !== 'pass') | |
{ | |
return $ret; | |
} | |
} | |
if(strtolower($env['PATH_INFO']) == '/about') | |
{ | |
$env['web.route_params'] = $merged = array ( | |
); | |
return array(302, array('Location' => 'http://www.google.com'), ''); | |
} | |
if(stripos($env['PATH_INFO'], '/search/') === 0) | |
{ | |
if(preg_match('#^/search/(?<query>\\w+)$#ui', $env['PATH_INFO'], $match)) | |
{ | |
$matches[] = $match; | |
$env['web.route_params'] = $merged = array_intersect_key(array_merge(array ( | |
), array_reduce($matches, 'array_merge', array())), array ( | |
'query' => 0, | |
)); | |
return array(301, array('Location' => 'http://www.google.com/search?q='.$merged['query']), ''); | |
array_pop($matches); | |
} | |
} | |
if(strtolower($env['PATH_INFO']) == '/test') | |
{ | |
if(isset($env['REQUEST_METHOD']) && $env['REQUEST_METHOD'] === 'GET') | |
{ | |
$env['web.route_params'] = $merged = array ( | |
'action' => 'index', | |
); | |
$ret = \Sample\Controller\Test::stack($engine, 'index')->run($env); | |
if(empty($ret[1]['X-Cascade']) OR $ret[1]['X-Cascade'] !== 'pass') | |
{ | |
return $ret; | |
} | |
} | |
if(isset($env['REQUEST_METHOD']) && $env['REQUEST_METHOD'] === 'POST') | |
{ | |
$env['web.route_params'] = $merged = array ( | |
'action' => 'index', | |
); | |
$ret = \Sample\Controller\Test::stack($engine, 'create')->run($env); | |
if(empty($ret[1]['X-Cascade']) OR $ret[1]['X-Cascade'] !== 'pass') | |
{ | |
return $ret; | |
} | |
} | |
$env['web.route_params'] = $merged = array ( | |
'action' => 'index', | |
); | |
$ret = \Sample\Controller\Test::stack($engine, 'lol')->run($env); | |
if(empty($ret[1]['X-Cascade']) OR $ret[1]['X-Cascade'] !== 'pass') | |
{ | |
return $ret; | |
} | |
} | |
if(strtolower($env['PATH_INFO']) == '/test/new') | |
{ | |
$env['web.route_params'] = $merged = array ( | |
'action' => 'index', | |
); | |
$ret = \Sample\Controller\Test::stack($engine, 'newform')->run($env); | |
if(empty($ret[1]['X-Cascade']) OR $ret[1]['X-Cascade'] !== 'pass') | |
{ | |
return $ret; | |
} | |
} | |
if(stripos($env['PATH_INFO'], '/test/') === 0) | |
{ | |
if(preg_match('#^/test/(?<test_id>\\w+)$#ui', $env['PATH_INFO'], $match)) | |
{ | |
$matches[] = $match; | |
if(isset($env['REQUEST_METHOD']) && $env['REQUEST_METHOD'] === 'GET') | |
{ | |
$env['web.route_params'] = $merged = array_intersect_key(array_merge(array ( | |
'action' => 'index', | |
), array_reduce($matches, 'array_merge', array())), array ( | |
'test_id' => 0, | |
)); | |
$ret = \Sample\Controller\Test::stack($engine, 'show')->run($env); | |
if(empty($ret[1]['X-Cascade']) OR $ret[1]['X-Cascade'] !== 'pass') | |
{ | |
return $ret; | |
} | |
} | |
if(isset($env['REQUEST_METHOD']) && $env['REQUEST_METHOD'] === 'PUT') | |
{ | |
$env['web.route_params'] = $merged = array_intersect_key(array_merge(array ( | |
'action' => 'index', | |
), array_reduce($matches, 'array_merge', array())), array ( | |
'test_id' => 0, | |
)); | |
$ret = \Sample\Controller\Test::stack($engine, 'update')->run($env); | |
if(empty($ret[1]['X-Cascade']) OR $ret[1]['X-Cascade'] !== 'pass') | |
{ | |
return $ret; | |
} | |
} | |
if(isset($env['REQUEST_METHOD']) && $env['REQUEST_METHOD'] === 'DELETE') | |
{ | |
$env['web.route_params'] = $merged = array_intersect_key(array_merge(array ( | |
'action' => 'index', | |
), array_reduce($matches, 'array_merge', array())), array ( | |
'test_id' => 0, | |
)); | |
$ret = \Sample\Controller\Test::stack($engine, 'destroy')->run($env); | |
if(empty($ret[1]['X-Cascade']) OR $ret[1]['X-Cascade'] !== 'pass') | |
{ | |
return $ret; | |
} | |
} | |
array_pop($matches); | |
} | |
if(preg_match('#^/test/(?<test_id>\\w+)/edit$#ui', $env['PATH_INFO'], $match)) | |
{ | |
$matches[] = $match; | |
$env['web.route_params'] = $merged = array_intersect_key(array_merge(array ( | |
'action' => 'index', | |
), array_reduce($matches, 'array_merge', array())), array ( | |
'test_id' => 0, | |
)); | |
$ret = \Sample\Controller\Test::stack($engine, 'edit')->run($env); | |
if(empty($ret[1]['X-Cascade']) OR $ret[1]['X-Cascade'] !== 'pass') | |
{ | |
return $ret; | |
} | |
array_pop($matches); | |
} | |
} | |
if(stripos($env['PATH_INFO'], '/admin') === 0) | |
{ | |
if(preg_match('#^/admin(?<uri>.*?)$#ui', $env['PATH_INFO'], $match)) | |
{ | |
$matches[] = $match; | |
$env['web.route_params'] = $merged = array_intersect_key(array_merge(array ( | |
), array_reduce($matches, 'array_merge', array())), array ( | |
'uri' => 0, | |
)); | |
$uri = $merged['uri']; | |
$path = empty($uri) ? $env['PATH_INFO'] : substr($env['PATH_INFO'], - strlen($uri)); | |
// Move one step deeper in the directory structure | |
$env['SCRIPT_NAME'] = $env['SCRIPT_NAME'].$path; | |
$env['BASE_URI'] = $env['BASE_URI'].$path; | |
$env['PATH_INFO'] = '/'.trim($uri, '/'); | |
$env['web.old_route_params'] = $env['web.route_params']; | |
$engine = new Admin\Engine(); | |
$ret = $engine->stack()->run($env); | |
if(empty($ret[1]['X-Cascade']) OR $ret[1]['X-Cascade'] !== 'pass') | |
{ | |
return $ret; | |
} | |
array_pop($matches); | |
} | |
} | |
if(preg_match('#^/(?<controller>blog|test)(?:/(?<action>\\w+)(?:/(?<id>\\w+))?)?(?:\\.(?<format>\\w+))?$#ui', $env['PATH_INFO'], $match)) | |
{ | |
$matches[] = $match; | |
$env['web.route_params'] = $merged = array_intersect_key(array_merge(array ( | |
'action' => 'index', | |
), array_reduce($matches, 'array_merge', array())), array ( | |
'controller' => 0, | |
'action' => 1, | |
'id' => 2, | |
'format' => 3, | |
)); | |
// No need to check if the index exists, the regex only matches available controllers | |
$class_name = $controllers[strtolower($merged['controller'])]; | |
$ret = $class_name::stack($engine, empty($merged['action']) ? 'index' : $merged['action'])->run($env); | |
if(empty($ret[1]['X-Cascade']) OR $ret[1]['X-Cascade'] !== 'pass') | |
{ | |
return $ret; | |
} | |
array_pop($matches); | |
} | |
return array(404, array('X-Cascade' => 'pass'), ''); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment