Zend\Expressive\Application::route PHP Method

route() public method

Accepts either a Router\Route instance, or a combination of a path and middleware, and optionally the HTTP methods allowed. On first invocation, pipes the route middleware to the middleware pipeline.
public route ( string | Zend\Expressive\Router\Route $path, callable | string | array $middleware = null, array $methods = null, null | string $name = null ) : Zend\Expressive\Router\Route
$path string | Zend\Expressive\Router\Route
$middleware callable | string | array Middleware (or middleware service name) to associate with route.
$methods array HTTP method to accept; null indicates any.
$name null | string the name of the route
return Zend\Expressive\Router\Route
    public function route($path, $middleware = null, array $methods = null, $name = null)
    {
        if (!$path instanceof Router\Route && null === $middleware) {
            throw new Exception\InvalidArgumentException(sprintf('%s expects either a route argument, or a combination of a path and middleware arguments', __METHOD__));
        }
        if ($path instanceof Router\Route) {
            $route = $path;
            $path = $route->getPath();
            $methods = $route->getAllowedMethods();
            $name = $route->getName();
        }
        $this->checkForDuplicateRoute($path, $methods);
        if (!isset($route)) {
            $methods = null === $methods ? Router\Route::HTTP_METHOD_ANY : $methods;
            $route = new Router\Route($path, $middleware, $methods, $name);
        }
        $this->routes[] = $route;
        $this->router->addRoute($route);
        return $route;
    }

Usage Example

コード例 #1
0
 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::route