src/Traits/PipelineTrait.php
public function withMiddleware(...$middleware): RouteInterface
    {
        $route = clone $this;

        // array fallback
        if (count($middleware) == 1 && is_array($middleware[0])) {
            $middleware = $middleware[0];
        }

        foreach ($middleware as $item) {
            if (!is_string($item) && !$item instanceof MiddlewareInterface) {
                if (is_object($item)) {
                    $name = get_class($item);
                } else {
                    $name = gettype($item);
                }

                throw new RouteException("Invalid middleware `{$name}`.");
            }

            $route->middleware[] = $item;
        }

        if (!empty($route->pipeline)) {
            $route->pipeline = $route->makePipeline();
        }

        return $route;
    }
public function withMiddleware(...$middleware): RouteInterface
    {
        $route = clone $this;

        // array fallback
        if (count($middleware) == 1 && is_array($middleware[0])) {
            $middleware = $middleware[0];
        }

        foreach ($middleware as $item) {
            if (!is_string($item) && !$item instanceof MiddlewareInterface) {
                if (is_object($item)) {
                    $name = get_class($item);
                } else {
                    $name = gettype($item);
                }

                throw new RouteException("Invalid middleware `{$name}`.");
            }

            $route->middleware[] = $item;
        }

        if (!empty($route->pipeline)) {
            $route->pipeline = $route->makePipeline();
        }

        return $route;
    }
public function withMiddleware ( ... $ middleware ) : RouteInterface { $ route = clone $ this ; // array fallback if ( count ( $ middleware ) == 1 && is_array ( $ middleware [ 0 ] ) ) { $ middleware = $ middleware [ 0 ] ; } foreach ( $ middleware as $ item ) { if ( ! is_string ( $ item ) && ! $ item instanceof MiddlewareInterface ) { if ( is_object ( $ item ) ) { $ name = get_class ( $ item ) ; } else { $ name = gettype ( $ item ) ; } throw new RouteException ( "Invalid middleware `{$name}`." ) ; } $ route -> middleware [ ] = $ item ; } if ( ! empty ( $ route -> pipeline ) ) { $ route -> pipeline = $ route -> makePipeline ( ) ; } return $ route ; }
Associated middleware with route. New instance of route will be returned.

Example:
$route->withMiddleware(new CacheMiddleware(100));
$route->withMiddleware(ProxyMiddleware::class);
$route->withMiddleware(ProxyMiddleware::class, OtherMiddleware::class);
$route->withMiddleware([ProxyMiddleware::class, OtherMiddleware::class]);

@param MiddlewareInterface|string|array ...$middleware
@return RouteInterface|$this

@throws RouteException
Associated middleware with route . New instance of route will be returned .