在Laravel 5条路线中消除{一?}/{二?}/{三?}/{四?}/{五?}字符串


Rid of {one?}/{two?}/{three?}/{four?}/{five?} string in Laravel 5 routes

我正在寻找一个选项,如何从路由中删除{一?}/{二?}-{三?}/{四?}和{五?}。这会自动添加到每条路线中像

v1/webapi/login/{one?}/{two?}/{three?}/{four?}/{five?}
v1/webapi/logout/{one?}/{two?}/{three?}/{four?}/{five?}

在路由文件m中使用以下路由

Route::group(['prefix' => 'v1', 'middleware' => 'webapi'], function(){
    Route::controller('webapi', 'WebApiController');
});

有人会有同样的问题吗?

简单的答案是-你不能删除它。这个方法https://github.com/laravel/framework/blob/5.2/src/Illuminate/Routing/ControllerInspector.php#L132添加那些通配符参数。

摆脱它的唯一方法是重写Router并使用自己的ControllerInspector,但这毫无意义,而且很难实现。


更新

但如果你真的想这么做,有一个棘手的解决方案:

    foreach ('Route::getRoutes() as $route) {
        $route->setUri(preg_replace('%'{one'?'}/'{two'?'}/'{three'?'}/'{four'?'}/'{five'?'}$%', '',
            $route->getUri()));
    }

将此代码放入RouteServiceProvider.php boot方法中。