Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从laravel中路由器实例上的路由调用before筛选器_Php_Laravel_Routing - Fatal编程技术网

Php 如何从laravel中路由器实例上的路由调用before筛选器

Php 如何从laravel中路由器实例上的路由调用before筛选器,php,laravel,routing,Php,Laravel,Routing,我可以这样称呼它 Route::get('profile', array('before' => 'auth', 'uses' => 'UserController@showProfile')); 路由器将使用之前的属性创建路由 在Route->run中调用以下命令 list($name, $params) = $this->parseFilter($name, $params); if ( ! is_null($callable = $this-&

我可以这样称呼它

Route::get('profile', array('before' => 'auth',
            'uses' => 'UserController@showProfile'));
路由器将使用
之前的属性创建路由

Route->run
中调用以下命令

list($name, $params) = $this->parseFilter($name, $params);

if ( ! is_null($callable = $this->router->getFilter($name)))
{
   return call_user_func_array($callable, $params);
}
我的问题是:路由器如何知道过滤器的名称。我是否遵循createRoute方法


谢谢

我不完全确定你在问什么-但拉威尔有两个过滤器-

在调用路由之前运行筛选器之前。您可以根据需要在
之前的
中附加任意多个筛选器,例如:
'before'=>'auth | other | example'
。在这种情况下,它将运行
auth
other
example
(按该顺序)

您可以在
过滤器之后对
执行完全相同的操作

此过滤器可以应用于routes文件,如示例中所示

Route::get('profile', array('before' => 'auth',
            'uses' => 'UserController@showProfile'));
或者它们可以应用于控制器内部(如果您喜欢的话)

class UserController extends BaseController {

    public function __construct()
    {
        $this->beforeFilter('auth');
    }
}
您还可以在控制器过滤器中指定过滤器仅应用于特定路由

public function __construct()
{
    $this->beforeFilter('csrf', array('on' => 'post'));
}
public function __construct()
{
    $this->beforeFilter('auth', array('except' => 'post'));
}
或者你可以说它适用于除特定路线以外的所有路线

public function __construct()
{
    $this->beforeFilter('csrf', array('on' => 'post'));
}
public function __construct()
{
    $this->beforeFilter('auth', array('except' => 'post'));
}

我相信您是在询问Laravel4.0,只是根据您提供的代码片段猜测而已。在Laravel 4.1中,
路由
模块中的代码发生了很大变化

路由器知道筛选器的名称,因为路由器保留该数据

让我们从创建before过滤器开始。在路由类中创建
before
filter
before()
方法时调用

public function before()
{
    $this->setBeforeFilters(func_get_args());

    return $this;
}
在此函数中,参数由函数读取

然后使用
setbeforiflters
方法将过滤器名称和过滤器参数设置到内部数据存储中


我希望这能回答你的问题。(如果我理解正确的话)

我相信OP非常熟悉如何使用过滤器。他的问题更多的是关于路由/过滤器的内部工作。正如我所说——我不完全确定他在问什么——这个问题可以用多种方式来解释。所以我试图提供尽可能多的关于路由和过滤器的信息。是的,我知道它是如何在内部存储的。这是个好答案。