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

查看:63
本文介绍了Laravel路线问题与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/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路线问题与web.php中的路线顺序有关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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