slimphp/Slim · error · RuntimeException

A middleware must be an object/class name referencing an imp

Error message

A middleware must be an object/class name referencing an implementation of MiddlewareInterface or a callable with a matching signature.

What it means

Error "A middleware must be an object/class name referencing an implementation of MiddlewareInterface or a callable with a matching signature." thrown in slimphp/Slim.

Source

Thrown at Slim/MiddlewareDispatcher.php:100

     *
     * @param MiddlewareInterface|string|callable $middleware
     */
    public function add($middleware): MiddlewareDispatcherInterface
    {
        if ($middleware instanceof MiddlewareInterface) {
            return $this->addMiddleware($middleware);
        }

        if (is_string($middleware)) {
            return $this->addDeferred($middleware);
        }

        if (is_callable($middleware)) {
            return $this->addCallable($middleware);
        }

        /** @phpstan-ignore-next-line */
        throw new RuntimeException(
            'A middleware must be an object/class name referencing an implementation of ' .
            'MiddlewareInterface or a callable with a matching signature.'
        );
    }

    /**
     * Add a new middleware to the stack
     *
     * Middleware are organized as a stack. That means middleware
     * that have been added before will be executed after the newly
     * added one (last in, first out).
     */
    public function addMiddleware(MiddlewareInterface $middleware): MiddlewareDispatcherInterface
    {
        $next = $this->tip;
        $this->tip = new class ($middleware, $next) implements RequestHandlerInterface {
            private MiddlewareInterface $middleware;

View on GitHub (pinned to 80900fb39c)

When it happens

Trigger: Thrown at Slim/MiddlewareDispatcher.php:100 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of slimphp/Slim@80900fb39c (2026-08-21). Data as JSON: /api/errors/d5353ed507e9cc33. Report an issue: GitHub.