From eb956cc89dd8f8748adffb1d404f0a7b5c5fd843 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 2 Oct 2011 23:28:30 -0500 Subject: [PATCH] some routing enhancements - still a work in progress. --- laravel/controller.php | 31 ++++++++++---- laravel/laravel.php | 2 +- laravel/routing/caller.php | 83 ++++++++++++++++---------------------- laravel/routing/route.php | 6 +-- laravel/routing/router.php | 34 ++++++++++++++++ 5 files changed, 94 insertions(+), 62 deletions(-) diff --git a/laravel/controller.php b/laravel/controller.php index 907ac1fd..97baf2cf 100644 --- a/laravel/controller.php +++ b/laravel/controller.php @@ -1,16 +1,31 @@ -$name; + } /** * Magic Method to handle calls to undefined functions on the controller. diff --git a/laravel/laravel.php b/laravel/laravel.php index a7e0bd03..85f28c94 100644 --- a/laravel/laravel.php +++ b/laravel/laravel.php @@ -39,8 +39,8 @@ if (Config::get('session.driver') !== '') * be returned to the browser. */ require SYS_PATH.'request'.EXT; -require SYS_PATH.'routing/route'.EXT; require SYS_PATH.'routing/router'.EXT; +require SYS_PATH.'routing/route'.EXT; require SYS_PATH.'routing/loader'.EXT; require SYS_PATH.'routing/caller'.EXT; diff --git a/laravel/routing/caller.php b/laravel/routing/caller.php index a76c302f..28061ca8 100644 --- a/laravel/routing/caller.php +++ b/laravel/routing/caller.php @@ -4,28 +4,6 @@ use Closure; use Laravel\Response; use Laravel\Container; -class Delegate { - - /** - * The destination of the route delegate. - * - * @var string - */ - public $destination; - - /** - * Create a new route delegate instance. - * - * @param string $destination - * @return void - */ - public function __construct($destination) - { - $this->destination = $destination; - } - -} - class Caller { /** @@ -85,30 +63,14 @@ class Caller { // If a route returns a Delegate, it also means the route is delegating the // handling of the request to a controller method. So, we will pass the string // to the route delegator, exploding on "@". - if ($response instanceof Delegate) $response = $this->delegate($route, $response->destination); + if ($response instanceof Delegate) return $this->delegate($route, $response->destination); return $this->finish($route, $response); } // If we get to this point, no response was returned from the filters or the route. // The 404 response will be returned to the browser instead of a blank screen. - return $this->finish($route, $this->container->resolve('laravel.response')->error('404')); - } - - /** - * Run the "before" filters for the route. - * - * If a before filter returns a value, that value will be considered the response to the - * request and the route function / controller will not be used to handle the request. - * - * @param Route $route - * @return mixed - */ - protected function before(Route $route) - { - $before = array_merge(array('before'), $route->filters('before')); - - return $this->filter($before, array(), true); + return $this->finish($route, Response::error('404')); } /** @@ -139,12 +101,17 @@ class Caller { $controller->container = $this->container; - // Again, as was the case with route closures, if the controller "before" method returns + // Again, as was the case with route closures, if the controller "before" filters return // a response, it will be considered the response to the request and the controller method // will not be used to handle the request to the application. - $response = $controller->before(); + $response = $this->before($controller); - return (is_null($response)) ? call_user_func_array(array($controller, $method), $route->parameters) : $response; + if (is_null($response)) + { + $response = call_user_func_array(array($controller, $method), $route->parameters); + } + + return $this->finish($controller, $response); } /** @@ -197,29 +164,47 @@ class Caller { * * The route response will be converted to a Response instance and the "after" filters will be run. * - * @param Route $route + * @param Destination $route * @param mixed $response * @return Response */ - protected function finish(Route $route, $response) + protected function finish(Destination $destination, $response) { if ( ! $response instanceof Response) $response = new Response($response); - $this->filter(array_merge($route->filters('after'), array('after')), array($response)); + $this->filter(array_merge($destination->filters('after'), array('after')), array($response)); return $response; } + /** + * Run the "before" filters for the routing destination. + * + * If a before filter returns a value, that value will be considered the response to the + * request and the route function / controller will not be used to handle the request. + * + * @param Route $route + * @return mixed + */ + protected function before(Destination $destination) + { + $before = array_merge(array('before'), $destination->filters('before')); + + return $this->filter($before, array(), true); + } + /** * Call a filter or set of filters. * - * @param array $filters - * @param array $parameters - * @param bool $override + * @param array|string $filters + * @param array $parameters + * @param bool $override * @return mixed */ protected function filter($filters, $parameters = array(), $override = false) { + if (is_string($filters)) $filters = explode('|', $filters); + foreach ((array) $filters as $filter) { // Parameters may be passed into routes by specifying the list of parameters after diff --git a/laravel/routing/route.php b/laravel/routing/route.php index b9a1f58e..58b57c3e 100644 --- a/laravel/routing/route.php +++ b/laravel/routing/route.php @@ -3,7 +3,7 @@ use Closure; use Laravel\Arr; -class Route { +class Route implements Destination { /** * The route key, including request method and URI. @@ -126,9 +126,7 @@ class Route { { if (is_array($this->callback) and isset($this->callback[$name])) { - $filters = $this->callback[$name]; - - return (is_string($filters)) ? explode('|', $filters) : $filters; + return $this->callback[$name]; } return array(); diff --git a/laravel/routing/router.php b/laravel/routing/router.php index 5951465c..557276b3 100644 --- a/laravel/routing/router.php +++ b/laravel/routing/router.php @@ -2,6 +2,40 @@ use Laravel\Request; +interface Destination { + + /** + * Get an array of filter names defined for the destination. + * + * @param string $name + * @return array + */ + public function filters($name); + +} + +class Delegate { + + /** + * The destination of the route delegate. + * + * @var string + */ + public $destination; + + /** + * Create a new route delegate instance. + * + * @param string $destination + * @return void + */ + public function __construct($destination) + { + $this->destination = $destination; + } + +} + class Router { /**