【问题标题】:Laravel 4 Routing with Arbitrary Number of URL Segments具有任意数量的 URL 段的 Laravel 4 路由
【发布时间】:2013-01-01 16:10:26
【问题描述】:

我有一个 L3 应用程序正在尝试移植到 L4。在 L3 版本中,我的一条路线是

Route::get('/(:any)/(:all?)', etc...

这让我可以处理任意数量的 URL 段,例如:

/contact_page
/store_category
/store_category/shirts_category
/store_category/shirts_category/specific_shirt_page
/an/arbitrary/number/of/nested/categories

但在 L4 中,我无法弄清楚如何模拟 (:all?) 的功能

以下代码有效:

Route::get('/{arg1?}/{arg2?}/{arg3?}', function($arg1='home', $arg2, $arg3)
{
  //do something
});

所以我可以添加大量可选参数(比我认为在实际使用中需要的更多)但这不是很优雅。

在 Laravel 4 中有什么方法可以定义一个可以响应任意数量的 URL 段的路由吗?

【问题讨论】:

    标签: php url-routing laravel laravel-4


    【解决方案1】:

    您可以在路线中添加模式条件,例如:

    Route::get('{any}/{args}', function($action, $args = null)
    {
       // do something like return print_r(explode('/', $args), true);
    })->where('args', '(.*)');
    

    【讨论】:

    • 太棒了!非常感谢。
    猜你喜欢
    • 2013-01-09
    • 1970-01-01
    • 2013-06-20
    • 2011-12-18
    • 1970-01-01
    • 2014-07-27
    • 2014-04-13
    • 2019-05-23
    • 2013-09-05
    相关资源
    最近更新 更多