laravel / folio

Page based routing for Laravel.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Terminable middleware not executed

zepfietje opened this issue · comments

Folio Version

v1.0.0-beta.2

Laravel Version

v10.17.1

PHP Version

8.2.8

Description

When registering terminable middleware in the FolioServiceProvider, the terminate() function of the middleware isn't executed.

Steps To Reproduce

  1. Create a terminable middleware class
  2. Add a log statement to the middleware's terminate() method: info('terminating')
  3. Register the middleware in the FolioServiceProvider:
    Folio::route(resource_path('views/pages'), middleware: [
        '*' => [
            TheTerminableMiddleware::class,
        ],
    ]);
  4. Load a folio page
  5. Check the logs
  6. Find no terminating log entry

Hi there,

Middleware is handled differently in Folio Router and even if we can merge the PR it would be slightly different from middleware registered to Laravel Router.

Alternatively, you can also run the following if possible.

    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        return tap($next($request), function ($response) use ($request) {
            // Run any terminating feature.
        });
    }

Thanks for taking a look at this issue, @crynobone!

Would your suggested code snippet actually run the code after returning the response though?
For now I've worked around it by using a regular middleware and app()->terminating().
Figured it'd be good to still report the issue as others might run into it too.

Okay, but is that behavior exactly the same as terminable middleware works?

Asking since I'm using it for tracking pageviews to an analytics service from the back end.
Calling the analytics API before the response is actually returned, results in performance issues in your app.

Terminable middleware is executed slightly later in the request lifecycle (terminating the app from HTTP Kernel) and may behave differently during feature tests. I myself haven't use terminable middleware.

No worries, @crynobone. Just want to make sure that terminable middleware runs in a similar fashion as it does when not using Folio. If that's impossible to achieve, it's probably better to not support it.