Ad

How To Resolve Urls Correctly In Laravel?

- 1 answer

I have url for product details (domain.com/productdetail/1/name-of-product), When i click "about us" page while still in this url, I get an error "this url doesnt exist"(domain.com/productdetail/1/about-us). How can i get the url to resolve correctly to domain.com/about-us

This is my productDetail route

Route::get('/productDetail/{id}/{pro_name}', 
'[email protected]');

this is about-us route

Route::get('about-us', function(){
return View('about');
});

I would like to get the exactly route (domain.com/about-us) instead of it chaining at the end of the current url (domain.com/productdetail/1/about-us).

Ad

Answer

Better to put routes as named routes.

Route::get('/productDetail/{id}/{pro_name}','[email protected]')->name('product.view');

Route::get('about-us', function(){
  return View('about');
})->name('about-us');

Call the specific route as

 <a target="_blank" rel="nofollow noreferrer" href="{{route('about-us')}}">about us</a>
 <a target="_blank" rel="nofollow noreferrer" href="{{route('product.view',['id' => $id, 'pro_name' => 'name_of_product'])}}">View Product</a>
Ad
source: stackoverflow.com
Ad