Last active
August 29, 2015 14:13
-
-
Save eimihar/52422bd9a145955b6c41 to your computer and use it in GitHub Desktop.
Objectified route
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 | |
// same api. example of adding routes. | |
$app->map->addRoute(array( | |
'r1'=> ['uri'=> 'first-level', 'subroute'=> array( | |
'sr2'=> ['uri'=> 'another-sub'] | |
)] | |
)); | |
// Get route 'r1'. \Exedra\Application\Map\Route | |
$route = $app->map->findByName('r1'); | |
// return \Exedra\Application\Map\Level (collection of routes), of that route. | |
$level = $route->getSubroute(); | |
// add one route directly by passing new instance of route. | |
// i am planning to use factory pattern later than directly passing this dependency. | |
$level->addRoute(new \Exedra\Application\Map\Route($level, 'sr2'))); | |
// add routes by array | |
$level->addRoutesByArray(array( | |
'sr3'=> ['uri'=> 'another-sub3'], | |
'sr4'=> ['uri'=> 'another-sub4', 'subroute'=> array( | |
'ssr5'=> ['uri'=> 'sub-sub5'] | |
)] | |
)); | |
// then later you can find the added route like : | |
$route = $app->map->findByName('r1.sr4'); | |
// print the route method like | |
echo $route->getParameter('uri'); // return another-sub4 | |
echo $route->getAbsoluteUri(); // return first-level/another-sub4 | |
// or alter route parameter. | |
$route->setParameter('uri', 'edited-sub4'); | |
$route->setUri('uri', 'edited-sub4'); // alias to above method. | |
// on the same level (r1), we can now find r1.sr4.ssr5 (by passing route name relative to it's level) | |
$route = $level->findRouteByName('sr4.ssr5'); | |
// you can set middleware handler directly or by pattern on a route. | |
$app->map->findByName('r1')->setMiddleware(function($exe) | |
{ | |
return $exe->next($exe); | |
}); | |
$app->map->findByName('r1')->setMiddleware('middleware=mymiddleware@handle'); | |
// so, on execution level, all execution, on that route or it's subroute, will execute the middleware along the way. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment