adding support for attaching filters based on URI routing.
Signed-off-by: Taylor Otwell <taylorotwell@gmail.com>
This commit is contained in:
@@ -98,8 +98,7 @@ switch (Request::method())
|
||||
$input = $_POST;
|
||||
break;
|
||||
|
||||
case 'PUT':
|
||||
case 'DELETE':
|
||||
default:
|
||||
if (Request::spoofed())
|
||||
{
|
||||
$input = $_POST;
|
||||
|
||||
@@ -38,14 +38,17 @@ class Filter {
|
||||
* Filter::register('before', array('Class', 'method'));
|
||||
* </code>
|
||||
*
|
||||
* @param string $name
|
||||
* @param Closure $callback
|
||||
* @param string $name
|
||||
* @param mixed $callback
|
||||
* @return void
|
||||
*/
|
||||
public static function register($name, Closure $callback)
|
||||
public static function register($name, $callback)
|
||||
{
|
||||
if (isset(static::$aliases[$name])) $name = static::$aliases[$name];
|
||||
|
||||
// If the filter starts with "pattern: ", the filter is being setup to match on
|
||||
// all requests that match a given pattern. This is nice for defining filters
|
||||
// that handle all URIs beginning with "admin" for example.
|
||||
if (starts_with($name, 'pattern: '))
|
||||
{
|
||||
foreach (explode(', ', substr($name, 9)) as $pattern)
|
||||
@@ -102,8 +105,7 @@ class Filter {
|
||||
|
||||
// We will also go ahead and start the bundle for the developer. This allows
|
||||
// the developer to specify bundle filters on routes without starting the
|
||||
// bundle manually, and performance is improved since the bundle is only
|
||||
// started when needed.
|
||||
// bundle manually, and performance is improved by lazy-loading.
|
||||
Bundle::start(Bundle::name($filter));
|
||||
|
||||
if ( ! isset(static::$filters[$filter])) continue;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php namespace Laravel\Routing;
|
||||
|
||||
use Closure;
|
||||
use Laravel\URI;
|
||||
use Laravel\Bundle;
|
||||
use Laravel\Request;
|
||||
use Laravel\Response;
|
||||
@@ -172,9 +173,38 @@ class Route {
|
||||
$filters = array_merge($filters, $assigned);
|
||||
}
|
||||
|
||||
// Next we will attach any pattern type filters to the array of
|
||||
// filters as these are matched to the route by the route's
|
||||
// URI and not explicitly attached to routes.
|
||||
if ($event == 'before')
|
||||
{
|
||||
$filters = array_merge($filters, $this->patterns());
|
||||
}
|
||||
|
||||
return array(new Filter_Collection($filters));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the pattern filters for the route.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function patterns()
|
||||
{
|
||||
// We will simply iterate through the registered patterns and
|
||||
// check the URI pattern against the URI for the route and
|
||||
// if they match we'll attach the filter.
|
||||
foreach (Filter::$patterns as $pattern => $filter)
|
||||
{
|
||||
if (URI::is($pattern, $this->uri))
|
||||
{
|
||||
$filters[] = $filter;
|
||||
}
|
||||
}
|
||||
|
||||
return (array) $filters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the controller action delegate assigned to the route.
|
||||
*
|
||||
@@ -340,11 +370,11 @@ class Route {
|
||||
/**
|
||||
* Register a route filter.
|
||||
*
|
||||
* @param string $name
|
||||
* @param Closure $callback
|
||||
* @param string $name
|
||||
* @param mixed $callback
|
||||
* @return void
|
||||
*/
|
||||
public static function filter($name, Closure $callback)
|
||||
public static function filter($name, $callback)
|
||||
{
|
||||
Filter::register($name, $callback);
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ class Router {
|
||||
'POST' => array(),
|
||||
'PUT' => array(),
|
||||
'DELETE' => array(),
|
||||
'PATCH' => array(),
|
||||
'HEAD' => array(),
|
||||
);
|
||||
|
||||
@@ -44,6 +45,7 @@ class Router {
|
||||
'POST' => array(),
|
||||
'PUT' => array(),
|
||||
'DELETE' => array(),
|
||||
'PATCH' => array(),
|
||||
'HEAD' => array(),
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user