【问题标题】:Laravel routes matching everything after prefixLaravel 路由匹配前缀后的所有内容
【发布时间】:2015-08-05 21:45:36
【问题描述】:

我正在尝试匹配 URL 中的路径,如下所示

我的 HTTP 请求

http://localhost/myprefix/extra/x/x/x/x/x/2/3/2/

routes.php

Route::group(
    ['prefix' => 'myprefix'],
    function () {
        Route::get('extra/{path}', ['as' => 'myprefix.one', 'uses' => 'MyController@index']);
        Route::get('extraOTher/{path}', ['as' => 'myprefix.two', 'uses' => 'MyController@indexOther']);
    }
);

MyController.php

public function index($path)
{
    // $path should be extra/x/x/x/x/x/2/3/2/
}

这一直给我错误

NotFoundHttpException in RouteCollection.php line 145:

我怎样才能让它工作?我在某处读到 :any 和 :all ,但我也无法让这些工作。

【问题讨论】:

    标签: laravel laravel-4 laravel-5 laravel-routing


    【解决方案1】:

    有点老套。

    Routes.php:

    Route::group(
        ['prefix' => 'myprefix'],
        function () {
            Route::get('extra/{path}', ['as' => 'myprefix.one', 'uses' => 'MyController@index']);
            Route::get('extraOTher/{path}', ['as' => 'myprefix.two', 'uses' => 'MyController@indexOther']);
        }
    );
    

    添加模式。

    Route::pattern('path', '[a-zA-Z0-9-/]+');
    

    现在它将捕获所有路线。

    Controller.php:

    public function index($path)
    {
        echo $path; // outputs x/x/x/2/3/4/ whatever there is. 
    
        // To get the prefix with all the segements,
    
        echo substr(parse_url(\Request::url())['path'],1);
    }
    

    不优雅。但它应该可以解决问题。

    【讨论】:

    • 嗨 itachi,我还是不能让它工作,你确定这能工作吗>
    • 啊,没关系!这是因为我有一个 .非常感谢!
    • 也为我工作
    猜你喜欢
    • 2020-09-09
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2022-01-24
    • 2018-11-30
    • 1970-01-01
    相关资源
    最近更新 更多