1

无论我做什么 crud middlware 总是被解雇。$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]);
    }
}
4

1 回答 1

3

查看 Laravel 代码,似乎当您使用:

$this->middleware('crud', ['only' => []]);

Laravel 将始终使用此中间件(适用于所有 Controller 方法),因此您不应使用带有空only选项的中间件。

所以你应该修改这个构造函数:

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();
}
于 2015-11-29T18:32:11.417 回答