Laravel 匹配前缀后所有内容的路由
Laravel routes matching everything after prefix
我正在尝试匹配 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 但我也无法使它们正常工作。
有点老套。
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);
}
不优雅。但它应该可以解决问题。
我正在尝试匹配 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 但我也无法使它们正常工作。
有点老套。
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);
}
不优雅。但它应该可以解决问题。