【问题标题】:Route resources set before auth not working in Laravel 4在身份验证之前设置的路由资源在 Laravel 4 中不起作用
【发布时间】:2013-07-03 02:05:45
【问题描述】:

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

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

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

class TicketController extends BaseController {

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

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

【问题讨论】:

    标签: laravel laravel-4


    【解决方案1】:

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

    \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;
        }
    

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

    如果你想在 route.php 中归档相同的结果,可以这样做

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

    【讨论】:

    • 或者你可以使用控制器的 beforeFilter() 方法。 $this->beforeFilter('auth', ['except' => 'destroy']);。查看德文在this link的评论
    猜你喜欢
    • 2014-02-04
    • 2015-07-28
    • 1970-01-01
    • 2016-02-05
    • 2013-04-17
    • 2015-02-25
    • 2013-12-11
    • 2014-10-18
    • 2020-12-29
    相关资源
    最近更新 更多