Commit 5325acac authored by Taylor Otwell's avatar Taylor Otwell

adding support for attaching filters based on URI routing.

Signed-off-by: 's avatarTaylor Otwell <taylorotwell@gmail.com>
parent 01ddff5c
...@@ -98,8 +98,7 @@ switch (Request::method()) ...@@ -98,8 +98,7 @@ switch (Request::method())
$input = $_POST; $input = $_POST;
break; break;
case 'PUT': default:
case 'DELETE':
if (Request::spoofed()) if (Request::spoofed())
{ {
$input = $_POST; $input = $_POST;
......
...@@ -38,14 +38,17 @@ class Filter { ...@@ -38,14 +38,17 @@ class Filter {
* Filter::register('before', array('Class', 'method')); * Filter::register('before', array('Class', 'method'));
* </code> * </code>
* *
* @param string $name * @param string $name
* @param Closure $callback * @param mixed $callback
* @return void * @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 (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: ')) if (starts_with($name, 'pattern: '))
{ {
foreach (explode(', ', substr($name, 9)) as $pattern) foreach (explode(', ', substr($name, 9)) as $pattern)
...@@ -102,8 +105,7 @@ class Filter { ...@@ -102,8 +105,7 @@ class Filter {
// We will also go ahead and start the bundle for the developer. This allows // 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 // the developer to specify bundle filters on routes without starting the
// bundle manually, and performance is improved since the bundle is only // bundle manually, and performance is improved by lazy-loading.
// started when needed.
Bundle::start(Bundle::name($filter)); Bundle::start(Bundle::name($filter));
if ( ! isset(static::$filters[$filter])) continue; if ( ! isset(static::$filters[$filter])) continue;
......
<?php namespace Laravel\Routing; <?php namespace Laravel\Routing;
use Closure; use Closure;
use Laravel\URI;
use Laravel\Bundle; use Laravel\Bundle;
use Laravel\Request; use Laravel\Request;
use Laravel\Response; use Laravel\Response;
...@@ -172,9 +173,38 @@ class Route { ...@@ -172,9 +173,38 @@ class Route {
$filters = array_merge($filters, $assigned); $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)); 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. * Get the controller action delegate assigned to the route.
* *
...@@ -340,11 +370,11 @@ class Route { ...@@ -340,11 +370,11 @@ class Route {
/** /**
* Register a route filter. * Register a route filter.
* *
* @param string $name * @param string $name
* @param Closure $callback * @param mixed $callback
* @return void * @return void
*/ */
public static function filter($name, Closure $callback) public static function filter($name, $callback)
{ {
Filter::register($name, $callback); Filter::register($name, $callback);
} }
......
...@@ -31,6 +31,7 @@ class Router { ...@@ -31,6 +31,7 @@ class Router {
'POST' => array(), 'POST' => array(),
'PUT' => array(), 'PUT' => array(),
'DELETE' => array(), 'DELETE' => array(),
'PATCH' => array(),
'HEAD' => array(), 'HEAD' => array(),
); );
...@@ -44,6 +45,7 @@ class Router { ...@@ -44,6 +45,7 @@ class Router {
'POST' => array(), 'POST' => array(),
'PUT' => array(), 'PUT' => array(),
'DELETE' => array(), 'DELETE' => array(),
'PATCH' => array(),
'HEAD' => array(), 'HEAD' => array(),
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment