refactoring routing classes.
This commit is contained in:
parent
bae9553aac
commit
2fae372c59
|
@ -62,8 +62,8 @@ public function call(Route $route)
|
||||||
{
|
{
|
||||||
// If a route returns a string, it also means the route is delegating the
|
// If a route returns a string, it also means the route is delegating the
|
||||||
// handling of the request to a controller method. So, we will pass the
|
// handling of the request to a controller method. So, we will pass the
|
||||||
// string to the route delegator, exploding on "::".
|
// string to the route delegator, exploding on "@".
|
||||||
if (is_string($response)) $response = $this->delegate($route, explode('::', $response));
|
if (is_string($response)) $response = $this->delegate($route, $response);
|
||||||
|
|
||||||
return $this->finish($route, $response);
|
return $this->finish($route, $response);
|
||||||
}
|
}
|
||||||
|
@ -92,13 +92,13 @@ protected function before(Route $route)
|
||||||
/**
|
/**
|
||||||
* Handle the delegation of a route to a controller method.
|
* Handle the delegation of a route to a controller method.
|
||||||
*
|
*
|
||||||
* @param Route $route
|
* @param Route $route
|
||||||
* @param array $delegate
|
* @param string $delegate
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
protected function delegate(Route $route, $delegate)
|
protected function delegate(Route $route, $delegate)
|
||||||
{
|
{
|
||||||
list($controller, $method) = array($delegate[0], $delegate[1]);
|
list($controller, $method) = explode('@', $delegate);
|
||||||
|
|
||||||
$controller = $this->resolve($controller);
|
$controller = $this->resolve($controller);
|
||||||
|
|
||||||
|
|
|
@ -67,7 +67,7 @@ public function __construct($key, $callback, $parameters = array())
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call the route closure.
|
* Call the closure defined for the route, or get the route delegator.
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -23,19 +23,19 @@ class Router {
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $controller_path;
|
protected $controllers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new router for a request method and URI.
|
* Create a new router for a request method and URI.
|
||||||
*
|
*
|
||||||
* @param array $routes
|
* @param array $routes
|
||||||
* @param string $controller_path
|
* @param string $controllers
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct($routes, $controller_path)
|
public function __construct($routes, $controllers)
|
||||||
{
|
{
|
||||||
$this->routes = $routes;
|
$this->routes = $routes;
|
||||||
$this->controller_path = $controller_path;
|
$this->controllers = $controllers;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -121,7 +121,7 @@ protected function route_to_controller(Request $request, $destination)
|
||||||
{
|
{
|
||||||
// If the request is to the root of the application, an ad-hoc route will be generated
|
// If the request is to the root of the application, an ad-hoc route will be generated
|
||||||
// to the home controller's "index" method, making it the default controller method.
|
// to the home controller's "index" method, making it the default controller method.
|
||||||
if ($request->uri() === '/') return new Route($request->method().' /', function() { return array('home', 'index'); });
|
if ($request->uri() === '/') return new Route($request->method().' /', 'home@index');
|
||||||
|
|
||||||
$segments = explode('/', trim($request->uri(), '/'));
|
$segments = explode('/', trim($request->uri(), '/'));
|
||||||
|
|
||||||
|
@ -142,12 +142,7 @@ protected function route_to_controller(Request $request, $destination)
|
||||||
// be used as the default controller method.
|
// be used as the default controller method.
|
||||||
$method = (count($segments) > 0) ? array_shift($segments) : 'index';
|
$method = (count($segments) > 0) ? array_shift($segments) : 'index';
|
||||||
|
|
||||||
// Now we're ready to dummy up a controller delegating route callback. This
|
return new Route($destination, $controller.'@'.$method, $segments);
|
||||||
// callback will look exactly like the callback the developer would create
|
|
||||||
// were they to code the controller delegation manually.
|
|
||||||
$callback = function() use ($controller, $method) { return array($controller, $method); };
|
|
||||||
|
|
||||||
return new Route($destination, $callback, $segments);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,7 +162,7 @@ protected function controller_key($segments)
|
||||||
{
|
{
|
||||||
foreach (array_reverse($segments, true) as $key => $value)
|
foreach (array_reverse($segments, true) as $key => $value)
|
||||||
{
|
{
|
||||||
if (file_exists($path = $this->controller_path.implode('/', array_slice($segments, 0, $key + 1)).EXT))
|
if (file_exists($path = $this->controllers.implode('/', array_slice($segments, 0, $key + 1)).EXT))
|
||||||
{
|
{
|
||||||
return $key + 1;
|
return $key + 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue