src/Valkyrja/Http/Middleware/MiddlewareAwareTrait.php
public function responseMiddleware(Request $request, Response $response, array $middleware = null)
    {
        // Set the middleware to any middleware passed or the base middleware
        $middleware = $middleware ?? self::$middleware;

        // Get the application
        $app = $this->getApplication();

        // Iterate through the middleware
        foreach ($middleware as $item) {
            // If the middleware is a group
            if ($this->isMiddlewareGroup($item)) {
                // Recurse into that middleware group
                $this->responseMiddleware(
                    $request,
                    $response,
                    $this->getMiddlewareGroup($item)
                );

                continue;
            }

            /* @var \Valkyrja\Http\Middleware\Middleware $item */
            $response = $item::after($request, $response);

            // Set the returned response in the container
            $app->container()->singleton(Response::class, $response);
        }

        return $response;
    }
public function responseMiddleware(Request $request, Response $response, array $middleware = null)
    {
        // Set the middleware to any middleware passed or the base middleware
        $middleware = $middleware ?? self::$middleware;

        // Get the application
        $app = $this->getApplication();

        // Iterate through the middleware
        foreach ($middleware as $item) {
            // If the middleware is a group
            if ($this->isMiddlewareGroup($item)) {
                // Recurse into that middleware group
                $this->responseMiddleware(
                    $request,
                    $response,
                    $this->getMiddlewareGroup($item)
                );

                continue;
            }

            /* @var \Valkyrja\Http\Middleware\Middleware $item */
            $response = $item::after($request, $response);

            // Set the returned response in the container
            $app->container()->singleton(Response::class, $response);
        }

        return $response;
    }
Dispatch after request processed middleware.

@param Request  $request    The request
@param Response $response   The response
@param array    $middleware [optional] The middleware to dispatch

@return mixed