Laravel 多嵌套相同的路由参数
Laravel multi-nested same route parameters
我在 Laravel 5.2 上创建互联网市场
需要在 url 中使用类别 slug 做类似面包屑的事情,
喜欢
/目录/{category_1}/{category_2}/{category_n}
但只获取最后一个参数并将其绑定到模型中
而且最后一个类别可以有产品标签和路线可以像
/目录/{category_1}/{category_2}/{category_n}/产品/{product_slug}
我的路由
Route::group(['prefix' => 'catalog'], function () {
Route::get('/', ['as' => 'shop_catalog', 'uses' => 'CatalogController@catalog']);
Route::group(['prefix' => '{category}'], function () {
Route::get('/', ['as' => 'shop_show_category', 'uses' => 'CategoryController@category']);
Route::group(['prefix' => 'product/{product}'], function () {
Route::get('/', ['as' => 'shop_show_product', 'uses' => 'ProductController@product']);
});
尝试在 RouteServiceProvider 中写入模式,如
$route->pattern('category', '.*');
然后按'/'展开并获取最后一个元素进行绑定。但是无法获取产品参数。
我该怎么做这个逻辑?
路线的顺序非常重要。
由于 shop_show_category
路由首先列出,路由器永远不会检查 url 是否更具体地匹配您的 shop_show_product
路由。
Route::group(['prefix' => '{category}'], function () {
Route::group(['prefix' => 'product/{product}'], function () {
Route::get('/', ['as' => 'shop_show_product', 'uses' => 'ProductController@product']);
});
Route::get('/', ['as' => 'shop_show_category', 'uses' => 'CategoryController@category']);
});
通过重新排列,使更具体的路由首先出现,您知道路由器将首先检查 shop_show_product
路由是否匹配。如果在 url 末尾看到 product/xxx
失败,它将尝试匹配 shop_show_category
路由。
我在 Laravel 5.2 上创建互联网市场 需要在 url 中使用类别 slug 做类似面包屑的事情,
喜欢
/目录/{category_1}/{category_2}/{category_n}
但只获取最后一个参数并将其绑定到模型中
而且最后一个类别可以有产品标签和路线可以像
/目录/{category_1}/{category_2}/{category_n}/产品/{product_slug}
我的路由
Route::group(['prefix' => 'catalog'], function () {
Route::get('/', ['as' => 'shop_catalog', 'uses' => 'CatalogController@catalog']);
Route::group(['prefix' => '{category}'], function () {
Route::get('/', ['as' => 'shop_show_category', 'uses' => 'CategoryController@category']);
Route::group(['prefix' => 'product/{product}'], function () {
Route::get('/', ['as' => 'shop_show_product', 'uses' => 'ProductController@product']);
});
尝试在 RouteServiceProvider 中写入模式,如
$route->pattern('category', '.*');
然后按'/'展开并获取最后一个元素进行绑定。但是无法获取产品参数。
我该怎么做这个逻辑?
路线的顺序非常重要。
由于 shop_show_category
路由首先列出,路由器永远不会检查 url 是否更具体地匹配您的 shop_show_product
路由。
Route::group(['prefix' => '{category}'], function () {
Route::group(['prefix' => 'product/{product}'], function () {
Route::get('/', ['as' => 'shop_show_product', 'uses' => 'ProductController@product']);
});
Route::get('/', ['as' => 'shop_show_category', 'uses' => 'CategoryController@category']);
});
通过重新排列,使更具体的路由首先出现,您知道路由器将首先检查 shop_show_product
路由是否匹配。如果在 url 末尾看到 product/xxx
失败,它将尝试匹配 shop_show_category
路由。