Laravel 来自 Controller 构造函数的中间件参数
Laravel Middleware parameters from Controller constructor
我想知道如何使用控制器构造函数设置中间件并引用中间件参数,因为我已在我的路由文件中成功完成。
我在 routes.php 中运行良好:
Route::group(['middleware' => 'user-type:user'], function () {
// routes
});
现在我想在控制器构造函数中执行此操作,但我遇到了一些问题...
public function __construct()
{
$this->middleware = 'event-is-active:voting';
}
当我访问 link 并应用上述内容时,出现以下错误:
ErrorException in ControllerDispatcher.php line 127:
Invalid argument supplied for foreach()
当然我做错了——我在文档中看不到如何做,阅读源代码也没有帮助,但也许我忽略了一些东西。所以我想知道什么是正确的方法,甚至可能吗?非常感谢任何帮助,谢谢!
试试这个
function __construct()
{
$this->middleware('user-type:param1,param2', ['only' => ['show', 'update']]);
}
您使用错误的语法从控制器构造函数设置中间件。
首先你必须使用laravel 5.1来使用中间件参数。
现在只能在controller的controller中设置中间件了
喜欢
function __construct()
{
$this->middleware('event-is-active:voting');//this will applies to all methods of your controller
$this->middleware('event-is-active:voting', ['only' => ['show', 'update']]);//this will applies only show,update methods of your controller
}
请注意上面代码中的 show 和 update 是示例名称。您必须写下您在控制器中使用的实际名称。
假设您正在使用
1. getShowUser($userId)
2. postUpdateUser($userId)
你必须在这些方法中应用中间件,如下所述:
function __construct()
{
$this->middleware('event-is-active:voting', ['only' => ['getShowUser', 'postUpdateUser']]);
}
我想知道如何使用控制器构造函数设置中间件并引用中间件参数,因为我已在我的路由文件中成功完成。
我在 routes.php 中运行良好:
Route::group(['middleware' => 'user-type:user'], function () {
// routes
});
现在我想在控制器构造函数中执行此操作,但我遇到了一些问题...
public function __construct()
{
$this->middleware = 'event-is-active:voting';
}
当我访问 link 并应用上述内容时,出现以下错误:
ErrorException in ControllerDispatcher.php line 127:
Invalid argument supplied for foreach()
当然我做错了——我在文档中看不到如何做,阅读源代码也没有帮助,但也许我忽略了一些东西。所以我想知道什么是正确的方法,甚至可能吗?非常感谢任何帮助,谢谢!
试试这个
function __construct()
{
$this->middleware('user-type:param1,param2', ['only' => ['show', 'update']]);
}
您使用错误的语法从控制器构造函数设置中间件。
首先你必须使用laravel 5.1来使用中间件参数。
现在只能在controller的controller中设置中间件了
喜欢
function __construct()
{
$this->middleware('event-is-active:voting');//this will applies to all methods of your controller
$this->middleware('event-is-active:voting', ['only' => ['show', 'update']]);//this will applies only show,update methods of your controller
}
请注意上面代码中的 show 和 update 是示例名称。您必须写下您在控制器中使用的实际名称。
假设您正在使用 1. getShowUser($userId) 2. postUpdateUser($userId)
你必须在这些方法中应用中间件,如下所述:
function __construct()
{
$this->middleware('event-is-active:voting', ['only' => ['getShowUser', 'postUpdateUser']]);
}