Reputation: 4675
I'm working on an e-commerce application. I require dynamic product/category urls according to their url_key (a column for url).
I'm trying to do something like this:
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');
}
}
Let us say we have urls like:
/about-us /contact-us
/sony-bravia-b32 (url key of a product) /tv-led (url key of a category)
Now I want to check that if a url exists in the "routes.php" file, then it should go to that url (with the data like if a form is submitted)
Otherwise, it checks if the url matches the url key of product or category and move there accordingly.
Sort of Magento behavior. I'm stuck at the if part.
Upvotes: 2
Views: 3647
Reputation: 5958
You can do that by using routes.php
.
This is the example how you could do it. In the real application you might need to create separate controller for each routes to handle the different behaviour of the application.
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
});
Make sure you put the static url above the dynamic one.
Upvotes: 2