Php 拉韦尔米德瓦雷';只有';每条路线都有火

Php 拉韦尔米德瓦雷';只有';每条路线都有火,php,laravel,laravel-5,laravel-routing,laravel-middleware,Php,Laravel,Laravel 5,Laravel Routing,Laravel Middleware,无论我做什么,都会被解雇。但是,仅当声明了$crud数组且仅针对其包含的路由时,才应激发该命令。但并非每次都是这样。即使我说$crud=[]但是,如果我声明['only'=>['route1','route2']]则它会按预期工作 <?php class BaseController extends Controller { /** * Routes which DO NOT load users notifications. * @var Array Rou

无论我做什么,都会被解雇。但是,仅当声明了
$crud
数组且仅针对其包含的路由时,才应激发该命令。但并非每次都是这样。即使我说
$crud=[]但是,如果我声明
['only'=>['route1','route2']]
则它会按预期工作

<?php

class BaseController extends Controller
{
    /**
     * Routes which DO NOT load users notifications.
     * @var Array Routes without notifications.
     */
    public $notifications;
    /**
     * Routes which DONT require users account to be configured.
     * @var Array Routes needing configuration.
     */
    public $configured;
    /**
     * Routes which REQUIRE ownership of resource.
     * @var Array CRUD routes.
     */
    public $crud;

    public function __construct()
    {
        $this->middleware('auth', ['except' => $this->routes]);
        $this->middleware('configured', ['except' => $this->configured]);
        $this->middleware('notifications', ['except' => $this->notifications]);
        $this->middleware('crud', ['only' => $this->crud]);
    }
}

查看Laravel代码,当您使用:

$this->middleware('crud', ['only' => []]);
Laravel将始终使用此中间件(对于所有控制器方法),因此您不应使用空
选项的中间件

因此,您应该修改此构造函数:

public function __construct()
{
    $this->middleware('auth', ['except' => $this->routes]);
    $this->middleware('configured', ['except' => $this->configured]);
    $this->middleware('notifications', ['except' => $this->notifications]);
    if ($this->crud) {
        $this->middleware('crud', ['only' => $this->crud]);
    }
}
在从
BaseController
扩展的子控制器中,您应该在构造函数中执行以下操作:

public function __construct() {
   // here you set values for properties
   $this->routes = ['a','b'];
   $this->configured = ['c'];
   $this->notifications = ['d'];
   $this->crud = ['e','f'];

   // here you run parent contructor
   parent::__construct();
}

多给点信息。因为在我看来,“除非”确实会在规定的路线之外开火。这是意料之中的。如果我理解错了,请进一步解释。我也有同样的想法,对空数组使用“only”,但直到现在我还找不到处理中间件选项的位置。请提供相应源代码的链接好吗?@shock\u gone\u wild查看
illumb\Routing\ControllerDispatcher
-methods
getMiddleware
methodExcludedByOptions
谢谢!你的答案绝对正确。(我已经+1了)。然而,我认为,实现的行为不是最优的。在源代码中!空($options['only'])应替换为!isset(…)如我的建议“仅将中间件应用于”nothing“(空数组)应该是有效的,并且不会在all@shock_gone_wild我看了一下,你是对的。必须是isset,但也必须是空的,只需跳过空的和非实例化的。我做了一些测试,昨天向laravels framework github发出了拉入请求,它被合并了,所以现在应该在最新版本中更新。@SterlingDuchess我打算在空闲时间做同样的事情:)