简体   繁体   English

在Laravel 4中将多个URL指向相同方法的最佳实践

[英]Best practice in pointing multiple urls to same method in Laravel 4

Is there a better way of doing this in Laravel 4 without repeating code? 在Laravel 4中,有没有更好的方法而不重复代码?

Route::get('site/{url}', 'SearchController@index');
Route::get('test1/{url}', 'SearchController@index');
Route::get('test2/{url}', 'SearchController@index');

ie pointing multiple urls to single method with query string values. 即使用查询字符串值将多个url指向单个方法。 What is the best practice to achieve this? 实现此目的的最佳实践是什么?

Thanks in advance for your suggestions. 预先感谢您的建议。

Make it one route with a dynamic first segment and restrict that to a set of values with regex: 使用动态第一个路段使其成为一条路线,并使用正则表达式将其限制为一组值:

Route::get('{first}/{url}', 'SearchController@index')
     ->where('first', '(site|test1|test2)');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM