.= 'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>php - 404 URLs does not pass through web Middlewares in Laravel 11, php8.2 and TwillCMS - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - 404 URLs does not pass through web Middlewares in Laravel 11, php8.2 and TwillCMS - Stack Overflow

programmeradmin1浏览0评论

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:

  1. 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
        ]);
  1. 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:

  1. 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
        ]);
  1. 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
Add a comment  | 

1 Answer 1

Reset to default 1

So 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.

发布评论

评论列表(0)

  1. 暂无评论