2

I have a working route as

Route::get('/{url}', 'Controller@view)->name('view')->where('url', '[\w\d\-]+(.*)');

This works perfectly and shows the page as below (just demo page): Demo Landing Page

I have a link on this page to place a order and I want to create a route as

Route::get('/{url}/order', 'Controller@order)->name('order')->where('url', '[\w\d\-]+(.*)');

View Blade

<a href="{{ route('order', $product['url']) }}">Order Now</a>

php artisan route:list

|    | GET|HEAD | {slug}        | view   | App\Http\Controllers\Controller@view    | web   |
|    | GET|HEAD | {slug}/order  | order  | App\Http\Controllers\Controller@order   | web   |

Whenever I click on this link Laravel returns Not Found Error. What is issue? I restarted server & checked for Mod_rewrite. Everything is fine.

4
  • 1
    Switch the position of your 2 routes.{slug}/order goes to {{slug}} because it fits the requirements. If you move the order above the regular slug it should work Commented Apr 3, 2018 at 8:03
  • @universal : Please show your value for $product['url'] Commented Apr 3, 2018 at 8:32
  • @Taacoo solution worked! Commented Apr 3, 2018 at 9:28
  • Ill make it an answer, which you can accept if you want @universal Commented Apr 3, 2018 at 9:34

2 Answers 2

2

Routes work from top to bottom. Laravel searches for anything that fits the given URL.

When using {{slugs}} or any other parameters it is key that you put the key with the widest range on the bottom of your routes.

Example:

Route::get('/{url}', 'Controller@index);
Route::get('/{url}/order', 'Controller@order);

With this setup. ALL the routes will go to your Controller@index method. Since your {{url}} catches everything

By switching the 2 your more strict route is met first before the url with a very broad requirements.

For more info check the laravel docs

Sign up to request clarification or add additional context in comments.

Comments

1

I think you forgot ' in your code Change your code

Route::get('/{url}/order', 'Controller@order)->name('order')->where('url', '[\w\d\-]+(.*)');

to

Route::get('/{url}/order', 'Controller@order')->name('order')->where('url', '[\w\d\-]+(.*)');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.