我正在Laravel 5.2中开发一个必须具有友好URL的应用程序。这不是常规方法的问题,其中{slug}通配符由控制器处理,但我想以不同的方式进行。
现在我只有两个控制器:
ProductsController@show
显示详细信息CategoriesController@show
显示所选类别以及分配给它的产品路线:
Route::get('product/{product}', 'ProductsCategory@show')->name('product.show');
Route::get('category/{category}', 'CategoriesController@show')->name('category.show');
所以,当我想要回应一个只使用route('product.show', compact('product'))
在我想处理从数据库中提取的不同URL之前没什么特别的。我认为可以创建另一个分配给现有的路由,当我使用route(...)
助手时,它将自动处理。但事实并非如此。例如,我有一个新的URL:
domain.com/new-url-for-product.html
因此,路由应该分配给具有一些ID的常规“product.show”路由,该路由由{product}通配符的路由模型绑定器处理。对于route(..)
助手来说,它应该打印友好的URL-s。
我不知道我的策略是否合适。你如何处理类似的问题?
答案 0 :(得分:0)
当然路线会自动处理。看看这个。我只是举个例子,您必须根据需要设置此代码。
route('product.show', 'product'=>'new-url-for-product.html');
将生成此
domain.com/new-url-for-product.html
route('product.show', 'product'=>'new-url2-for-product.html');
将生成此网址
domain.com/new-url2-for-product.html
依此类推,然后你必须在你的控制器方法中处理所有这些。
例如:此路线的控制器方法为ProductsCategory@show
public function show($product){
if($product == 'new-url-for-product.html'){
//perform this
}
if($product == 'new-url2-for-product.html'){
//perform this
}
}
这只是一个例子,您必须根据需要进行映射
已编辑以下是我测试的示例
Route::get('/product/{product}.html', ['as'=>'product.show', 'uses'=>'ProductsCategory@show']);