Laravel Route 问题与 web.php 中的路由顺序 [英] Laravel Route issues with Route order in web.php

查看:24
本文介绍了Laravel Route 问题与 web.php 中的路由顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Laravel 中遇到路由问题,我正在学习一个教程,我们在 web.php 文件中列出了这些路由

I have problem with routes in Laravel, I'm following one tutorial and we have this routes listed in web.php file

Route::get('/home', 'HomeController@index')->name('home');
Route::get('/blog', 'BlogController@index')->name('blog');
Route::get('/blog/create', 'BlogController@create');
Route::post('/blog/store', 'BlogController@store');
Route::get('/blog/{id}', 'BlogController@show');
Route::get('/blog/{id}/edit', 'BlogController@edit');
Route::patch('/blog/{id}', 'BlogController@update');
Route::delete('/blog/{id}', 'BlogController@destroy');
Route::get('/blog/bin', 'BlogController@bin');

问题出在最后一个到 blog/bin 的路径中,如果我把它放在下面就行不通了,但是在教程中我们已经把它移到了其他路径的顶部,然后它工作正常,讲师说有一些冲突路线,最后一条路线需要在顶部才能工作,但根本没有解释为什么?任何人都可以更详细地解释一下,因为我真的刚开始使用 Laravel...

Problem is in the last route to blog/bin, it is not working if I keep it down below however in tutorial we have moved it to top of other routes and then it's working fine, instructor said that there is some conflict in routes and that that last route need to be at top in order to work but didn't explain at all why ? Can anybody explain in little more details, as I'm really just started with Laravel...

推荐答案

当访问一条路由时,Laravel 会从上到下遍历你的路由列表,直到找到一个匹配"的路由,此时这条路由会被立即选中.

When accessing a route, Laravel goes through your list of routes top to bottom, until it finds one that 'matches' at which point this route is immediately selected.

在您的示例中,当尝试使用 GET 访问 /blog/bin 时,它有两个潜在的匹配项:

In your example, when trying to access /blog/bin using GET, it has two potential matches:

Route::get('/blog/{id}', 'BlogController@show');

Route::get('/blog/bin', 'BlogController@bin');

在这种情况下,Route::get('/blog/{id}', 'BlogController@show'); 排在第一位,因此它会被选中.

In this case, Route::get('/blog/{id}', 'BlogController@show'); comes first so it would be selected.

正如前面的答案正确指出的那样,将 /blog/bin 路由放在 /blog/{id} 路由上方可以解决问题.然而,这个解决方案"让你在未来容易犯类似的错误(例如,当定义一个 /blog/example 路由并意外地将它放在 /blog/{id}).另外,我个人认为让你的路由的功能依赖于放置它们的顺序不是很优雅.

As the previous answers correctly state, placing the /blog/bin route above the /blog/{id} route would solve the problem. However, this 'solution' leaves you open to a similar mistake in the future (when, for example, defining a /blog/example route and accidentally placing it under /blog/{id}). In addition, I personally think it is not very elegant to have the functioning of your routes depend on the order to place them in.

在我看来,如果可能,更可靠的解决方案是限制/blog/{id} 接受的可能值,并带有 正则表达式约束.

In my opinion, when possible, a more robust solution is to restrict the possible values that are accepted by /blog/{id} with a regex constraint.

例如,如果您在博客文章中使用数字 ID,那么您知道如果 id 是一个路由 /blog/{id}数字.因此,您可以按如下方式定义路线:

For example, if you are using a numeric ID for your blogposts, you know that you only want to use route /blog/{id} if id is a number. Thus, you would define your route as follows:

Route::get('/blog/{id}', 'BlogController@show')->where('id', '[0-9]+');

当然,这通常是不可能的,例如,如果您使用帖子标题作为 id,但是如果有某种方法可以将帖子 id 与任何其他 /blog/foo 路线,那么这将是一种可能性.

Of course this is often not a possibility, for example if you use the post title as an id, but if there is some way to differentiate a post id from any other /blog/foo route, then this would be a possibility.

这篇关于Laravel Route 问题与 web.php 中的路由顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆