Zend\Expressive\Application::pipe PHP Method

pipe() public method

Middleware piped may be either callables or service names. Middleware specified as services will be wrapped in a closure similar to the following: function ($request, $response, $next = null) use ($container, $middleware) { $invokable = $container->get($middleware); if (! is_callable($invokable)) { throw new Exception\InvalidMiddlewareException(sprintf( 'Lazy-loaded middleware "%s" is not invokable', $middleware )); } return $invokable($request, $response, $next); }; This is done to delay fetching the middleware until it is actually used; the upshot is that you will not be notified if the service is invalid to use as middleware until runtime. Middleware may also be passed as an array; each item in the array must resolve to middleware eventually (i.e., callable or service name). Finally, ensures that the route middleware is only ever registered once.
public pipe ( string | array | callable $path, null | string | array | callable $middleware = null ) : self
$path string | array | callable Either a URI path prefix, or middleware.
$middleware null | string | array | callable Middleware
return self
    public function pipe($path, $middleware = null)
    {
        if (null === $middleware) {
            $middleware = $this->prepareMiddleware($path, $this->container);
            $path = '/';
        }
        if (!is_callable($middleware) && (is_string($middleware) || is_array($middleware))) {
            $middleware = $this->prepareMiddleware($middleware, $this->container);
        }
        if ($middleware === [$this, 'routeMiddleware'] && $this->routeMiddlewareIsRegistered) {
            return $this;
        }
        if ($middleware === [$this, 'dispatchMiddleware'] && $this->dispatchMiddlewareIsRegistered) {
            return $this;
        }
        parent::pipe($path, $middleware);
        if ($middleware === [$this, 'routeMiddleware']) {
            $this->routeMiddlewareIsRegistered = true;
        }
        if ($middleware === [$this, 'dispatchMiddleware']) {
            $this->dispatchMiddlewareIsRegistered = true;
        }
        return $this;
    }

Usage Example

 public static function setUpBeforeClass()
 {
     // Load configuration
     $config = (require __DIR__ . '/../config/config.php');
     // Override config settings
     $config['debug'] = true;
     $config['config_cache_enabled'] = false;
     $dependencies = $config['dependencies'];
     $dependencies['services']['config'] = $config;
     // Build container
     self::$container = new ServiceManager($dependencies);
     // Get application from container
     self::$app = self::$container->get(Application::class);
     self::$app->raiseThrowables();
     // Setup middleware
     self::$app->pipe(ServerUrlMiddleware::class);
     self::$app->pipe(ErrorHandler::class);
     self::$app->pipe(SessionMiddleware::class);
     self::$app->pipeRoutingMiddleware();
     self::$app->pipe(UrlHelperMiddleware::class);
     self::$app->pipeDispatchMiddleware();
     self::$app->pipe(NotFoundHandler::class);
     // Setup routes
     self::$app->route('/', Action\HomePageAction::class, ['GET'], 'home');
     self::$app->route('/blog', Action\BlogIndexAction::class, ['GET'], 'blog');
     self::$app->route('/blog/feed.xml', Action\BlogXmlFeedAction::class, ['GET'], 'feed');
     self::$app->route('/blog/{id:[0-9a-zA-Z\\-]+}', Action\BlogPostAction::class, ['GET'], 'blog.post');
     self::$app->route('/code', Action\CodeAction::class, ['GET'], 'code');
     self::$app->route('/contact', Action\ContactAction::class, ['GET', 'POST'], 'contact');
 }
All Usage Examples Of Zend\Expressive\Application::pipe