I have a redirect middleware. This middleware checks if the path is defined in our Redirection module table.
Normally, when a page exists (200) say http://localhost/existing-page, I can redirect the page to any url I want.
However any url that resolves to (404), does not pass through this middleware hence the rediretions does not work.
I tried following:
- Add redirect middleware to 'web' middleware group in app.php under bootstrap. 404 URLs did not pass trough eventhough all existing routes including Twill's admin routes did:
$middleware->group('web', [
...
\App\Http\Middleware\Redirector::class
]);
- I defined a
Route::fallback()
inside my web.php, outside my standart route groups and as the last and the first route declaration inside the file, did not pass through.
Route::fallback(function () {
dd('404');
return view("404"); // template should exists
})->middleware('redirect');
Keep in mind that my dynamic route handlers defined as follows in their controllers:
public function show(Page $page): View|RedirectResponse
{
...
}
I think my knowledge on Laravel's exception handlers and Middlewares is insufficient. Any help is appriciated.
I have a redirect middleware. This middleware checks if the path is defined in our Redirection module table.
Normally, when a page exists (200) say http://localhost/existing-page, I can redirect the page to any url I want.
However any url that resolves to (404), does not pass through this middleware hence the rediretions does not work.
I tried following:
- Add redirect middleware to 'web' middleware group in app.php under bootstrap. 404 URLs did not pass trough eventhough all existing routes including Twill's admin routes did:
$middleware->group('web', [
...
\App\Http\Middleware\Redirector::class
]);
- I defined a
Route::fallback()
inside my web.php, outside my standart route groups and as the last and the first route declaration inside the file, did not pass through.
Route::fallback(function () {
dd('404');
return view("404"); // template should exists
})->middleware('redirect');
Keep in mind that my dynamic route handlers defined as follows in their controllers:
public function show(Page $page): View|RedirectResponse
{
...
}
I think my knowledge on Laravel's exception handlers and Middlewares is insufficient. Any help is appriciated.
Share Improve this question edited Mar 11 at 14:33 Antonio Carlos Ribeiro 87.8k22 gold badges220 silver badges205 bronze badges asked Jan 30 at 12:24 ExarillionExarillion 637 bronze badges 4- laravel/docs/11.x/routing#fallback-routes suggests you can only add, not remove middlewares to the fallback route. It inherits all middlewares from the web group. – hakre Commented Jan 30 at 12:39
- Yes it was a typing / autocorrect mistake. – Exarillion Commented Jan 30 at 17:01
- 1 Does it work now? Is your redirect middleware on the 404 you were looking for? If not, please create a minimal middleware example (e.g. always rick-rolling but better just some example URL the mappnig should be irrelevant for your question) so that it becomes more clear what you are trying to do. Also debug by removing other middlewares temporarily to rule out any side-effects by them. If you found a culprit, edit it also in, it is important in the context of your question. – hakre Commented Jan 30 at 17:19
- 1 @hakre Hi, I have found the issue in the flow, I'll be uploading my answer shortly. I appreciate your efforts. – Exarillion Commented Jan 30 at 17:41
1 Answer
Reset to default 1So I have found what the problem is.
Basically, I was registering my Redirection middleware only in the routes/web.php
which is actually at the end of the lifecycle.
However, I use Route Model Binding for aforementioned system. and group them as follows:
Route::group([
'prefix' => LaravelLocalization::setLocale(),
'middleware' => ['redirect','noIndexNoFollow','localize', 'localizationRedirect', 'localeViewPath'],
], function () {
#Route Declarations
});
Then I found out that there was a group of Middleware that I was not able to see in bootstrap/app.php
One of them being \Illuminate\Routing\Middleware\SubstituteBindings::class
.
This Middleware basically makes the connection between a slug / ID with It's respective Model. Since this random URLs have no record in database, The Middleware fires 404 Exception. Which blocks request flow to the aforementioned group and it's Middlewares.
So what I did was as mentioned in Laravel's documentation added these default Middlewares to my bootstrap/app.php
and added the Redirection Middleware before SubstituteBindings Middleware as follows:
<?php
...
->withMiddleware(callback: function (Middleware $middleware) {
$middleware->group('web', [
\Illuminate\Cookie\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class,
**Redirector::class,**
\Illuminate\Routing\Middleware\SubstituteBindings::class, // this allow Laravel to binde slugs / ids etc. to their respoective models in the controllers.
// \Illuminate\Session\Middleware\AuthenticateSession::class,
]);
...
After that it worked like a charm.