我在做一个电子商务应用程序。我需要动态产品/类别url根据他们的url_key (一个列的url)。
我想做这样的事:
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');
}
}
让我们说我们有这样的网址:
/关于-我们/联系-我们
/sony-bravia-b32 (产品的url键) /tv-led (类别的url键)
现在,我想检查一下,如果"routes.php“文件中存在一个url,那么它应该转到那个url (包含类似于表单提交的数据)。
否则,它将检查url是否与产品或类别的url键相匹配,并相应地移到那里。
马根托的行为。我被困在if部分了。
发布于 2015-04-06 18:55:05
您可以通过使用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置于动态url之上。
https://stackoverflow.com/questions/29477292
复制相似问题