Laravel combining routes

帅比萌擦擦* 提交于 2019-12-11 19:41:54

问题


Is there any way of combining these two following routes?

Route::get('project/(:any)', 'dashboard.project@(:1)');
Route::get('project/(:any)/(:any)', 'dashboard.project@(:1)');

I'm asking because the second route, I had to add it in order for the following requests to work:

site.com/project/new => method
site.com/project/view/12351326 => method and parameter

回答1:


You can use regex in the first parameter of the get() method to combine those.

See section Regular Expression Route Constraints in the docs.

Something like this might work:

Route::get('project/{name}', function($name)
{
    //
})
->where('name', '[A-Za-z0-9\/]+');

Where the regex will match URLs such as /projects/something/anything. However that might be too loose - it will also match /projects/any/number/of/url/segments. But I believe some more RegEx can help make better constraints to get closer to what you're looking for.



来源:https://stackoverflow.com/questions/17043745/laravel-combining-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!