Laravel 路由到错误的控制器方法
Laravel routing to wrong controller method
我有以下 2 条路线:
路线 1:
Route::get(config('api.basepath') . '{username}/{hash}/{object_id}', [
'action' => 'getObject',
'uses' => 'ObjectController@getObject',
]);
和
路线2:
Route::get(config('api.basepath') . 'object-boxes/{object_id}/boxes', function () {
if (Input::get('action') == 'filter') {
return App::call('App\Http\Controllers\FilterController@getFilteredContents');
} else {
return App::call('App\Http\Controllers\ObjectBoxController@show');
}
});
现在,
路由 1 可以使用或不使用 Auth 中间件,而路由 2 在 Auth 中间件下。
但是,在任何一个调用中,执行控制都会转到 ObjectController@getObject
当我将 Route1 移动到 Route2 下面时,每次调用都会转到 ObjectBoxController@show。
我试过 Preceed 但没有任何改变。
我该如何解决这个问题
您应该定义路由中的变量,而不是每个路由末尾的 where
:
Route::get(config('api.basepath') . '{username}/{hash}/{object_id}', [
'action' => 'getObject',
'uses' => 'ObjectController@getObject',
])->where('object_id', '[0-9]+');
Route::get(config('api.basepath') . 'object-boxes/{object_id}/boxes', function () {
if (Input::get('action') == 'filter') {
return App::call('App\Http\Controllers\FilterController@getFilteredContents');
} else {
return App::call('App\Http\Controllers\ObjectBoxController@show');
}
})->where('object_id', '[0-9]+');
You may constrain the format of your route parameters using the where method on a route instance. The where method accepts the name of the parameter and a regular expression defining how the parameter should be constrained:
你的第一条路线和第二条路线相似,
第一条路线
config('api.basepath') . '{username}/{hash}/{object_id}'
第二条路线
config('api.basepath') . 'object-boxes/{object_id}/boxes'
当你有第二个路由案例时,它被视为前一个,并将 username
视为 object-boxes
,将 object_id
视为 boxes
。所以两种情况都调用相同的函数。因此,请尝试为这两条路线使用不同的路线模式。
我有以下 2 条路线:
路线 1:
Route::get(config('api.basepath') . '{username}/{hash}/{object_id}', [
'action' => 'getObject',
'uses' => 'ObjectController@getObject',
]);
和
路线2:
Route::get(config('api.basepath') . 'object-boxes/{object_id}/boxes', function () {
if (Input::get('action') == 'filter') {
return App::call('App\Http\Controllers\FilterController@getFilteredContents');
} else {
return App::call('App\Http\Controllers\ObjectBoxController@show');
}
});
现在,
路由 1 可以使用或不使用 Auth 中间件,而路由 2 在 Auth 中间件下。
但是,在任何一个调用中,执行控制都会转到 ObjectController@getObject
当我将 Route1 移动到 Route2 下面时,每次调用都会转到 ObjectBoxController@show。
我试过 Preceed 但没有任何改变。 我该如何解决这个问题
您应该定义路由中的变量,而不是每个路由末尾的 where
:
Route::get(config('api.basepath') . '{username}/{hash}/{object_id}', [
'action' => 'getObject',
'uses' => 'ObjectController@getObject',
])->where('object_id', '[0-9]+');
Route::get(config('api.basepath') . 'object-boxes/{object_id}/boxes', function () {
if (Input::get('action') == 'filter') {
return App::call('App\Http\Controllers\FilterController@getFilteredContents');
} else {
return App::call('App\Http\Controllers\ObjectBoxController@show');
}
})->where('object_id', '[0-9]+');
You may constrain the format of your route parameters using the where method on a route instance. The where method accepts the name of the parameter and a regular expression defining how the parameter should be constrained:
你的第一条路线和第二条路线相似,
第一条路线
config('api.basepath') . '{username}/{hash}/{object_id}'
第二条路线
config('api.basepath') . 'object-boxes/{object_id}/boxes'
当你有第二个路由案例时,它被视为前一个,并将 username
视为 object-boxes
,将 object_id
视为 boxes
。所以两种情况都调用相同的函数。因此,请尝试为这两条路线使用不同的路线模式。