Laravel动态路由
问题描述:
我正在开发电子商务应用程序。我需要根据url_key(url的一列)动态产品/类别url。Laravel动态路由
我试图做这样的事情:
if (Route::has('route.name')) {
// go where ever it is intended
}
else {
$productHelper = new ProductHelper();
$product = $productHelper->getProductByUrl($url_key);
if ($product)
return Redirect::to('product')->with('id', $product->id);
else {
$categoryHelper = new CategoryHelper();
$category = $categoryHelper->getCategoryByUrl($url_key);
if ($category)
return Redirect::route('category')->with('id', $category->id);
else
return View::make('static.page_not_found');
}
}
让我们说我们有像URL:
/约 - 我们 /接触我们
/索尼BRAVIA -b32(产品的url密钥) /电视主导(一个类别的URL键)
现在我想检查,如果一个url存在于“routes.ph p“文件,那么它应该去那个网址(与数据一样,如果提交表格)
否则,它会检查url是否匹配产品或类别的url key并相应地移动。
Magento行为的排序。我卡在if部分。
答
您可以使用routes.php
来实现。
这是你怎么做的例子。在实际的应用程序中,您可能需要为每个路径创建单独的控制器来处理应用程序的不同行为。
routes.php文件
Route::get('/about-us', function(){
echo 'About Us';
});
Route::get('/contact-us', function(){
echo 'Contact Us';
});
Route::get('/{product}/{category}', function($product, $category){
dd($product,$category); // remove this line and put your business logic here
});
确保你把静态URL上方的动态之一。
感谢mininoz,把动态路线放在最后解决了它。 – Ashutosh