[英]Laravel explicit route mode binding not working in some cases?
I am using Laravel 8 and explicit route mode binding in my project to call an product under the slug
instead of the id
.我在我的项目中使用 Laravel 8 和显式路由模式绑定来调用
slug
而不是id
下的产品。 I have products, posts and etc, which are all using explicit route mode binding with the slug
in the database instead of the id
.我有产品、帖子等,它们都使用显式路由模式绑定数据库中的
slug
而不是id
。
What really confuses me is that the explicit route mode binding is working everywhere but not with products
.真正让我困惑的是,显式路由模式绑定在任何地方都有效,但不适用于
products
。 Here is my RouteServiceProvider:这是我的 RouteServiceProvider:
// Change route for product from $id to $slug
Route::bind('product', function ($value) {
return Product::where('slug', $value)->first() ?? abort(404);
});
// Change route for user from $id to $name
Route::bind('user', function ($value) {
return User::where('name', $value)->first() ?? abort(404);
});
// Change route for product from $id to $slug
Route::bind('post', function ($value) {
return Post::where('slug', $value)->first() ?? abort(404);
});
Here are some example routes
for a user
, a product
and a post
:以下是
user
、 product
和post
一些示例routes
:
works: Route::get('/product/{productType}/{product}', ['as' => 'product.show', 'uses' => 'ProductController@show']);
404 error: Route::post('product/{product}/like', ['as' => 'product.like', 'uses' => 'LikeController@likeProduct']);
404 error: Route::post('product/{product}/dislike', ['as' => 'product.dislike', 'uses' => 'LikeController@dislikeProduct']);
404 error: Route::post('product/{product}/mark', ['as' => 'product.mark', 'uses' => 'ProductController@markProduct']);
works: Route::get('user/{user}', ['as' => 'user.show', 'uses' => 'UserController@show']);
works: Route::post('user/{user}/update/password', ['as' => 'user.password.change', 'uses' => 'UserController@updatePassword']);
works: Route::get('/blog/{postCategory}/{post}', ['as' => 'post.show', 'uses' => 'PostController@show']);
When I do a dd
for the value
in the RouteServiceProvider for user
, product
and post
I can figure out what's the problem here.当我为
user
、 product
和post
的 RouteServiceProvider 中的value
执行dd
,我可以找出这里的问题所在。 All values
do dump the actual slug
of the resource.所有
values
都会转储资源的实际slug
。 Even the get
request for a product is the actual slug
.甚至对产品的
get
请求也是实际的slug
。 However, when performing an post
request for a product
, I always get the end of the url
as value.但是,在对
product
执行post
请求时,我总是将url
的结尾作为值。 Of course this leads to a 404
.当然,这会导致
404
。
For example the route product/{product}/mark
with an post
requests does dump mark
as value
and not the slug
of the product... why is that?例如,带有
post
请求的路由product/{product}/mark
确实将mark
转储为value
而不是产品的slug
......为什么会这样? All other post
requests work fine!所有其他
post
请求工作正常!
I also tried switch a route from a post
method to a get
method but same result here... also an 404
error and I get mark
as value
again...我还尝试将路由从
post
方法切换到get
方法,但这里的结果相同......也是404
错误,我再次mark
为value
......
Can anybody explain whats the problem here and why it is not working?任何人都可以解释这里的问题是什么以及为什么它不起作用?
When I change product/{product}/mark
to produkt/{product}/mark
it is working.当我将
product/{product}/mark
更改为produkt/{product}/mark
它正在工作。 But why?但为什么? and why is '/product/{productType}/{product}' working?
为什么 '/product/{productType}/{product}' 有效? That is really strange..
那真的很奇怪。。
Kind regards and thank you!亲切的问候和谢谢!
我现在已经删除了RouteServiceProvider
更改,并且我正在使用自 Laravel 8 以来提供的此功能。问题解决了......我不知道问题是什么......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.