some routing enhancements - still a work in progress.
This commit is contained in:
parent
7be960fb6f
commit
eb956cc89d
|
@ -1,16 +1,31 @@
|
|||
<?php namespace Laravel;
|
||||
<?php namespace Laravel; use Laravel\Routing\Destination;
|
||||
|
||||
abstract class Controller {
|
||||
abstract class Controller implements Destination {
|
||||
|
||||
/**
|
||||
* A stub method that will be called before every request to the controller.
|
||||
* The "before" filters defined for the controller.
|
||||
*
|
||||
* If a value is returned by the method, it will be halt the request cycle
|
||||
* and will be considered the response to the request.
|
||||
*
|
||||
* @return mixed
|
||||
* @var array
|
||||
*/
|
||||
public function before() {}
|
||||
public $before = array();
|
||||
|
||||
/**
|
||||
* The "after" filters defined for the controller.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $after = array();
|
||||
|
||||
/**
|
||||
* Get an array of filter names defined for the destination.
|
||||
*
|
||||
* @param string $name
|
||||
* @return array
|
||||
*/
|
||||
public function filters($name)
|
||||
{
|
||||
return $this->$name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic Method to handle calls to undefined functions on the controller.
|
||||
|
|
|
@ -39,8 +39,8 @@
|
|||
* 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;
|
||||
|
||||
|
|
|
@ -4,28 +4,6 @@
|
|||
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 @@ public function call(Route $route)
|
|||
// 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 @@ protected function delegate(Route $route, $delegate)
|
|||
|
||||
$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 @@ protected function load($controller)
|
|||
*
|
||||
* 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
|
||||
|
|
|
@ -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 @@ public function filters($name)
|
|||
{
|
||||
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();
|
||||
|
|
|
@ -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 {
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue