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 |
Результат | Zend\Expressive\Router\Route |
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'); }
/** * Inject routes from configuration, if any. * * @param Application $app * @param ContainerInterface $container */ private function injectRoutes(Application $app, ContainerInterface $container) { $config = $container->has('Config') ? $container->get('Config') : []; if (!isset($config['routes'])) { return; } foreach ($config['routes'] as $spec) { if (!isset($spec['path']) || !isset($spec['middleware'])) { continue; } $methods = isset($spec['allowed_methods']) && is_array($spec['allowed_methods']) ? $spec['allowed_methods'] : Route::HTTP_METHOD_ANY; $route = $app->route($spec['path'], $spec['middleware'], $methods); if (isset($spec['options']) && is_array($spec['options'])) { $route->setOptions($spec['options']); } } }
/** * Inject routes from configuration, if any. * * @param array $routes Route definitions * @param Application $app */ private function injectRoutes(array $routes, Application $app) { foreach ($routes as $spec) { if (!isset($spec['path']) || !isset($spec['middleware'])) { continue; } if (isset($spec['allowed_methods'])) { $methods = $spec['allowed_methods']; if (!is_array($methods)) { throw new ContainerInvalidArgumentException(sprintf('Allowed HTTP methods for a route must be in form of an array; received "%s"', gettype($methods))); } } else { $methods = Route::HTTP_METHOD_ANY; } $name = isset($spec['name']) ? $spec['name'] : null; $route = new Route($spec['path'], $spec['middleware'], $methods, $name); if (isset($spec['options'])) { $options = $spec['options']; if (!is_array($options)) { throw new ContainerInvalidArgumentException(sprintf('Route options must be an array; received "%s"', gettype($options))); } $route->setOptions($options); } $app->route($route); } }
/** * Inject routes from configuration, if any. * * @param Application $app * @param ContainerInterface $container */ private function injectRoutes(Application $app, ContainerInterface $container) { $config = $container->has('config') ? $container->get('config') : []; if (!isset($config['routes'])) { $app->pipeRoutingMiddleware(); return; } foreach ($config['routes'] as $spec) { if (!isset($spec['path']) || !isset($spec['middleware'])) { continue; } $methods = isset($spec['allowed_methods']) && is_array($spec['allowed_methods']) ? $spec['allowed_methods'] : null; $name = isset($spec['name']) ? $spec['name'] : null; $route = $app->route($spec['path'], $spec['middleware'], $methods, $name); if (isset($spec['options']) && is_array($spec['options'])) { $route->setOptions($spec['options']); } } }
/** * @dataProvider routerAdapters * @group 74 */ public function testWithOnlyRootPathRouteDefinedRoutingToSubPathsShouldReturn404($adapter) { $app = new Application(new $adapter()); $app->route('/', function ($req, $res, $next) { $res->getBody()->write('Middleware'); return $res; }, ['GET']); $next = function ($req, $res) { return $res; }; $request = new ServerRequest(['REQUEST_METHOD' => 'GET'], [], '/foo', 'GET'); $response = new Response(); $result = $app->routeMiddleware($request, $response, $next); $this->assertInstanceOf(Response::class, $result); $this->assertNotEquals(405, $result->getStatusCode()); }
/** * Inject routes from configuration, if any. * * @param Application $app * @param ContainerInterface $container */ private function injectRoutes(Application $app, ContainerInterface $container) { $config = $container->has('config') ? $container->get('config') : []; if (!isset($config['routes'])) { $app->pipeRoutingMiddleware(); return; } foreach ($config['routes'] as $spec) { if (!isset($spec['path']) || !isset($spec['middleware'])) { continue; } if (isset($spec['allowed_methods'])) { $methods = $spec['allowed_methods']; if (!is_array($methods)) { throw new ContainerInvalidArgumentException(sprintf('Allowed HTTP methods for a route must be in form of an array; received "%s"', gettype($methods))); } } else { $methods = Route::HTTP_METHOD_ANY; } $name = isset($spec['name']) ? $spec['name'] : null; $route = new Route($spec['path'], $spec['middleware'], $methods, $name); if (isset($spec['options'])) { $options = $spec['options']; if (!is_array($options)) { throw new ContainerInvalidArgumentException(sprintf('Route options must be an array; received "%s"', gettype($options))); } $route->setOptions($options); } $app->route($route); } }