身份验证之前设置的路由资源在 Laravel 4 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17438599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:04:21  来源:igfitidea点击:

Route resources set before auth not working in Laravel 4

laravellaravel-4

提问by Shiro

I added this in routes.php, expected it will check the authentication session for the page, however it is not working.

我在routes.php 中添加了这个,期望它会检查页面的身份验证会话,但是它不起作用。

Route::resource('ticket', 'TicketController', array('before' => 'auth') );

Then I go to the controller, work in another way. It's work.

然后我转到控制器,以另一种方式工作。这是工作。

class TicketController extends BaseController {

public function __construct()
{
    $this->beforeFilter('auth');
}

May I know where can get more documentation regarding the Route::resource(), what type of argument it able to accept?

我可以知道在哪里可以获得更多关于 Route::resource() 的文档,它可以接受什么类型的参数?

回答by Shiro

OK... I found the answer.

好的...我找到了答案。

in

\vendor\laravel\framework\src\Illuminate\Routing\Router.php

\vendor\laravel\framework\src\Illuminate\Routing\Router.php

public function resource($resource, $controller, array $options = array())
    {
        // If the resource name contains a slash, we will assume the developer wishes to
        // register these resource routes with a prefix so we will set that up out of
        // the box so they don't have to mess with it. Otherwise, we will continue.
        if (str_contains($resource, '/'))
        {
            $this->prefixedResource($resource, $controller, $options);

            return;
        }

        // We need to extract the base resource from the resource name. Nested resources
        // are supported in the framework, but we need to know what name to use for a
        // place-holder on the route wildcards, which should be the base resources.
        $base = $this->getBaseResource($resource);

        $defaults = $this->resourceDefaults;

        foreach ($this->getResourceMethods($defaults, $options) as $method)
        {
            $this->{'addResource'.ucfirst($method)}($resource, $base, $controller);
        }
    }

protected function getResourceMethods($defaults, $options)
    {
        if (isset($options['only']))
        {
            return array_intersect($defaults, $options['only']);
        }
        elseif (isset($options['except']))
        {
            return array_diff($defaults, $options['except']);
        }

        return $defaults;
    }

as you can see, it only accept onlyand exceptarguement only.

如您所见,它只接受onlyexcept争论。

If you want to archive the same result in route.php, it can be done as below

如果你想在 route.php 中存档相同的结果,可以如下完成

Route::group(array('before'=>'auth'), function() {   
    Route::resource('ticket', 'TicketController');
});