更新时间:2023-02-21 17:46:22
因此,如您所见,参数是按顺序而不是按名称传递给函数的.
So, as you've seen the parameters are passed to the function in order, not by name.
要实现所需的功能,您可以在函数中通过如下类型提示请求对象来访问这些路由参数:
To achieve what you want, you can access these route parameters from within your function by type hinting the request object to it like this:
class ProductController extends Controller
{
function list(Request $request){ # <----------- don't pass the params, just the request object
$page = $request->route('page'); # <--- Then access by name
$category = $request->route('category');
dd("Page: $page | Category: $category");
}
}
然后,您当然可以将所有3条路线设置为使用相同的控制器方法:
Then of course you would set all 3 of your routes to hit that same controller method:
Route::get('/{category}/Page{page}', 'ProductController@list');
Route::get('/Page{page}', 'ProductController@list');
Route::get('/{category}', 'ProductController@list');
希望这会有所帮助..!
Hope this helps..!