laravel 如果参数不是整数,我如何以不同的方式定义路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30414860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:37:07  来源:igfitidea点击:

How can I define a route differently if parameter is not integer

phplaravelroutes

提问by Matthieu Boisjoli

I am using Laravel 5 and working on my local. I made a route with a parameter of {id} and another route with a specific name like so :

我正在使用 Laravel 5 并在我的本地工作。我创建了一个参数为 {id} 的路由和另一个具有特定名称的路由,如下所示:

Route::get('contacts/{id}', 'ContactController@get_contact');
Route::get('contacts/new', 'ContactController@new_contact');

My problem here is that if I try to go at localhost/contacts/new it will automatically access to the get_contact method. I understand that I have made a {id} parameter but what if I want to call get_contact only if my parameter is an integer? If it is not, check if it's "new" and access to new_contact method. Then, if it's not an integer and not "new", error page 404.

我的问题是,如果我尝试访问 localhost/contacts/new,它将自动访问 get_contact 方法。我知道我已经创建了一个 {id} 参数,但是如果我只想在我的参数是整数时调用 get_contact 怎么办?如果不是,请检查它是否是“新的”并访问 new_contact 方法。然后,如果它不是整数且不是“新的”,则会出现错误页面 404。

How can I do that in Laravel 5?

我怎样才能在 Laravel 5 中做到这一点?

Thanks for your help!

谢谢你的帮助!

回答by Limon Monte

Just add ->where('id', '[0-9]+')to route where you want to accept number-only parameter:

只需添加->where('id', '[0-9]+')到要接受仅数字参数的路由:

Route::get('contacts/{id}', 'ContactController@get_contact')->where('id', '[0-9]+');
Route::get('contacts/new', 'ContactController@new_contact');

Read more: http://laravel.com/docs/master/routing#route-parameters

阅读更多:http: //laravel.com/docs/master/routing#route-parameters

回答by Hafiz

A simple solution would be to use an explicit approach.

一个简单的解决方案是使用显式方法。

Route::get('contacts/{id:[0-9]+}', 'ContactController@get_contact');
Route::get('contacts/new', 'ContactController@new_contact');

回答by Johannes Maria Frank

Although the accepted answer is perfectly fine, usually a parameter is used more than once and thus you might want to use a DRY approach by defining a pattern in your bootfunction in the RouteServiceProvider.phpfile located under app/Providers(Laravel 5.3 and onwards):

尽管接受的答案非常好,但通常一个参数会被多次使用,因此您可能希望通过bootRouteServiceProvider.php位于app/Providers(Laravel 5.3 及更高版本)下的文件中的函数中定义模式来使用 DRY 方法:

 /**
 * Define your route model bindings, pattern filters, etc.
 *
 * @return void
 */
public function boot()
{
    Route::pattern('id', '[0-9]+');

    parent::boot();
}

This way, whereever you use your {id}parameter the constraints apply.

这样,无论您{id}在哪里使用参数,约束都适用。

回答by Inuyaki

There is also the possibility to just switch those around, because route file will go through all lines from top to bottom until it finds a valid route.

也有可能只是切换它们,因为路由文件将从上到下遍历所有行,直到找到有效的路由。

Route::get('contacts/new', 'ContactController@new_contact');
Route::get('contacts/{id}', 'ContactController@get_contact');

If you want to restrict that route to pure numbers, the marked solution is correct though.

如果您想将该路线限制为纯数字,则标记的解决方案是正确的。

Just adding it here, I know it is quite old ;)

只是在这里添加它,我知道它已经很旧了;)