Error running middleware closure example

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

/**
* Instantiate App
*
* In order for the factory to work you need to ensure you have installed
* a supported PSR-7 implementation of your choice e.g.: Slim PSR-7 and a supported
* ServerRequest creator (included with Slim PSR-7)
*/
$app = AppFactory::create();

/**
 * Example middleware closure
 *
 * @param  Request        $request PSR-7 request
 * @param  RequestHandler $handler PSR-15 request handler
 *
 * @return ResponseInterface
 */
$beforeMiddleware = function (Request $request, RequestHandler $handler) use ($app) {
    $response = $handler->handle($request);
    $existingContent = (string) $response->getBody();

    $response = $app->getResponseFactory()->createResponse();
    $response->getBody()->write('BEFORE' . $existingContent);

    return $response;
};

$afterMiddleware = function ($request, $handler) {
    $response = $handler->handle($request);
    $response->getBody()->write('AFTER');
    return $response;
};

$app->add($beforeMiddleware);
$app->add($afterMiddleware);


/**
* The routing middleware should be added earlier than the ErrorMiddleware
* Otherwise exceptions thrown from it will not be handled by the middleware
*/
$app->addRoutingMiddleware();

/**
* Add Error Middleware
*
* @param bool                  $displayErrorDetails -> Should be set to false in production
* @param bool                  $logErrors -> Parameter is passed to the default ErrorHandler
* @param bool                  $logErrorDetails -> Display error details in error log
* @param LoggerInterface|null  $logger -> Optional PSR-3 Logger
*
* Note: This middleware should be added last. It will not handle any exceptions/errors
* for middleware added after it.
*/
$errorMiddleware = $app->addErrorMiddleware(true, true, true);

// Define app routes
$app->get('/', function (Request $request, Response $response, $args) {
$name = $args['name'];
$response->getBody()->write("Hello, world");
return $response;
});

// Run app
$app->run();

error
 Type: TypeError
   Code: 0
   Message: Argument 2 passed to {closure}() must be an instance of RequestHandler, instance of Slim\Routing\RouteRunner given, called in /var/www/html/slim420240401/vendor/slim/slim/Slim/MiddlewareDispatcher.php on line 269
   File: /var/www/html/slim420240401/public/index.php
   Line: 25

Trace

#0 /var/www/html/slim420240401/vendor/slim/slim/Slim/MiddlewareDispatcher.php(269): {closure}(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner))
#1 /var/www/html/slim420240401/public/index.php(36): class@anonymous->handle(Object(Slim\Psr7\Request))
#2 /var/www/html/slim420240401/vendor/slim/slim/Slim/MiddlewareDispatcher.php(269): {closure}(Object(Slim\Psr7\Request), Object(class@anonymous))
#3 /var/www/html/slim420240401/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php(45): class@anonymous->handle(Object(Slim\Psr7\Request))
#4 /var/www/html/slim420240401/vendor/slim/slim/Slim/MiddlewareDispatcher.php(121): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(class@anonymous))
#5 /var/www/html/slim420240401/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(76): class@anonymous->handle(Object(Slim\Psr7\Request))
#6 /var/www/html/slim420240401/vendor/slim/slim/Slim/MiddlewareDispatcher.php(121): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(class@anonymous))
#7 /var/www/html/slim420240401/vendor/slim/slim/Slim/MiddlewareDispatcher.php(65): class@anonymous->handle(Object(Slim\Psr7\Request))
#8 /var/www/html/slim420240401/vendor/slim/slim/Slim/App.php(199): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
#9 /var/www/html/slim420240401/vendor/slim/slim/Slim/App.php(183): Slim\App->handle(Object(Slim\Psr7\Request))
#10 /var/www/html/slim420240401/public/index.php(72): Slim\App->run()
#11 {main}


To resolve this issue, you need to ensure that the middleware closures are defined properly with the correct type-hint for the second argument, which should be RequestHandlerInterface instead of RequestHandler.

use Psr\Http\Server\RequestHandlerInterface;
// ...

$beforeMiddleware = function (Request $request, RequestHandlerInterface $handler) use ($app) {

// ...

thankyou for the hint now works fine

thias mean that documentation at

https://www.slimframework.com/docs/v4/concepts/middleware.html#closure-middleware-example

it is wrong

Update: The documentation seems to be correct.

image