Laravel 中间件 'only' 为每个路由触发
Laravel middlware 'only' fires for every route
无论我做什么,crud 中间件总是被解雇。但是,只有在声明了 $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 将始终使用此中间件(用于所有 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();
}
无论我做什么,crud 中间件总是被解雇。但是,只有在声明了 $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 将始终使用此中间件(用于所有 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();
}