skip to Main Content

For some reason, I need to map /* to a Controller in Laravel. I am using below mapping:

Route::get('/{slug}', [MyController::class, 'index']);

It works fine for all URLs tested, but when I access example.com/javascript
Instead of calling Controller, it returns 403 – Forbidden. I do not have ‘javascript’ folder inside the ‘public’ folder.

How to configure Laravel, it shall go to routes and delegate request to related Controller?

2

Answers


  1. If you add parameter it’ll works fine otherwise by deafult laravel route mechanism can’t understand. please try to put "/p" Short parameter before your slug url.

    Make sure to clear your route cache after making changes to your routes

    Login or Signup to reply.
  2. I am going to assume that you have registered this route in your web.php route file, since you do not specify /api/ in the example url.

    If you have other routes registered such as Route::get('/some-route', [ExampleController::class, 'index']); it will work fine when you visit example.com/some-route, but everything else will fall into the Route::get('/{slug}', [MyController::class, 'index']); route, meaning the framework will assume that whatever you write after example.com/ is the {slug}, and then it likely fails an auth check in your MyController class for this reason.

    I hope this clarify things for you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search