Laravel组合路线 [英] Laravel combining routes

查看:79
本文介绍了Laravel组合路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以将以下两条路线结合起来?

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

推荐答案

您可以在get()方法的第一个参数中使用正则表达式来组合它们.

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

请参阅文档中的正则表达式路由约束.

类似的事情可能会起作用:

Something like this might work:

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

正则表达式将与诸如/projects/something/anything之类的URL匹配的位置.但是,这可能太宽松了-它也将匹配/projects/any/number/of/url/segments.但是我相信更多的RegEx可以帮助制定更好的约束条件,以使其更接近您所要查找的内容.

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.

这篇关于Laravel组合路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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