[英]How can I add url extensions to laravel routes with optional parameters?
I have the following routes for a webshop with categories and products: 对于具有类别和产品的网上商店,我有以下路线:
Route::name('shop.products.view')->get('/p/{productUrl}', 'Shop\ProductsController@view');
Route::name('shop.categories.view')->get('/c/{categoryOne}/{categoryTwo?}/{categoryThree?}', 'Shop\CategoriesController@view')
categoryTwo is a subcategory of categoryOne categoryTwo是categoryOne的子类别
categoryThree is a subcategory of categoryTwo categoryThree是categoryTwo的子类别
This works perfect but I need to put .html at the end so the url's are exactly the same as the url's from the old webshop. 这可以完美地工作,但是我需要将.html放在末尾,以便url与旧网络商店中的url完全相同。
For the productpage this is no problem: 对于productpage,这是没有问题的:
Route::name('shop.products.view')->get('/p/{productUrl}.html', 'Shop\ProductsController@view');
If I do this for the category page it doesn't work when the optional parameters are not filled. 如果我对类别页面执行此操作,则在未填充可选参数时将无法使用。
Route::name('shop.categories.view')->get('/c/{categoryOne}/{categoryTwo?}/{categoryThree?}.html', 'Shop\CategoriesController@view')
This results in: domain.com/c/category1//.html 结果是: domain.com/c/category1//.html
Any ideas on how to solve this so I get: 关于如何解决这个问题的任何想法,我得到:
domain.com/c/category1.html domain.com/c/category1.html
domain.com/c/category1/category2.html domain.com/c/category1/category2.html
domain.com/c/category1/category2/category3.html domain.com/c/category1/category2/category3.html
You have two options: 您有两种选择:
.html
, and pass them as ?category2=aaa&category3=bbb
; 在.html
之后,使用category2和category3作为查询参数,并将其作为?category2=aaa&category3=bbb
传递; URL::action('Shop\\CategoriesController@view')
. 我不喜欢这种解决方案,但是如果您正确地调用了路由而不是从URL构建器URL::action('Shop\\CategoriesController@view')
调用,它应该可以工作。 Route::name('shop.products.view.')->group(function () {
Route::get('/c/{categoryOne}/{categoryTwo}/{categoryThree}.html', 'Shop\CategoriesController@view');
Route::get('/c/{categoryOne}/{categoryTwo}.html', 'Shop\CategoriesController@view');
Route::get('/c/{categoryOne}.html', 'Shop\CategoriesController@view')
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.