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
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
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 visitexample.com/some-route
, but everything else will fall into theRoute::get('/{slug}', [MyController::class, 'index']);
route, meaning the framework will assume that whatever you write afterexample.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.