[英]Laravel 5 override route resource parameters
I'm using some web routes behind a resource controller. 我在资源控制器后面使用了一些Web路由。 So the route looks simply like:
所以这条路线看起来很简单:
Route::resource('account', 'AccountController');
I also have a web middleware that checks if a user_id
parameter is set in the Request
object: 我还有一个Web中间件,用于检查
Request
对象中是否设置了user_id
参数:
if ($request->user_id) {
// do stuff
}
The problem is this. 问题是这个。 In my non-resource routes I use something like:
在我的非资源路线中,我使用如下:
Route::get('agentnotes/{user_id}', 'UserNoteController@getUserNotes');
This sets the user_id
variable as expected, and the middleware functions fine. 这将
user_id
变量设置为预期,并且中间件功能正常。
But in the resource routes, even though the actual route method uses the user_id
, the middleware isn't seeing it. 但是在资源路由中,即使实际路由方法使用
user_id
,中间件也没有看到它。 So for example, the AccountController::show
method looks like this: 例如,
AccountController::show
方法如下所示:
public function show($user_id)
But the middleware doesn't see that user_id
as part of the request, I assume because it's already fired before the request gets to the controller. 但是中间件没有看到
user_id
是请求的一部分,我假设因为它已经在请求到达控制器之前被触发了。
Is there a way to handle this without rewriting all the resource routes? 有没有办法处理这个而不重写所有资源路由?
According to the Laravel docs: 根据Laravel文档:
By default, Route::resource will create the route parameters for your resource routes based on the "singularized" version of the resource name.
默认情况下,Route :: resource将根据资源名称的“单一化”版本为您的资源路由创建路由参数。 You can easily override this on a per resource basis by passing parameters in the options array.
您可以通过在options数组中传递参数,在每个资源的基础上轻松覆盖它。
So I had to override the default values using my user_id
that the middleware expected. 所以我不得不使用中间件预期的
user_id
来覆盖默认值。
Route::resource('account', 'AccountController', ['parameters' => [
'account' => 'user_id'
]]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.