本文整理汇总了PHP中FastRoute\simpleDispatcher函数的典型用法代码示例。如果您正苦于以下问题:PHP simpleDispatcher函数的具体用法?PHP simpleDispatcher怎么用?PHP simpleDispatcher使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了simpleDispatcher函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: initRoutes
/**
* Initialize the route dispatcher and defined routes.
*/
protected function initRoutes()
{
$this->dispatcher = FastRoute\simpleDispatcher(function (RouteCollector $r) {
$r->addRoute('GET', '/', 'IndexController@index');
$r->addRoute('GET', '/{id:\\d+}', 'IndexController@withId');
});
}
示例2: __construct
/**
* Router constructor.
*/
public function __construct()
{
$dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $collector) {
$collector->addRoute(['GET'], '/logout', [new \App\Controllers\AdminController(), 'logout']);
$collector->addRoute(['GET', 'POST'], '/[{table}[/{action}[/{id}]]]', [new \App\Controllers\AdminController(), 'main']);
});
$factory = new Abimo\Factory();
$request = $factory->request();
$method = $request->method();
$uri = $request->uri();
$route = $dispatcher->dispatch($method, $uri);
switch ($route[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
$response = $factory->response();
$response->header('404', true, 404)->send();
throw new \ErrorException("Route {$method} {$uri} not found.");
break;
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
$response = $factory->response();
$response->header('405', true, 405)->send();
throw new \ErrorException("Method {$method} not allowed.");
break;
case FastRoute\Dispatcher::FOUND:
$handler = $route[1];
$arguments = $route[2];
call_user_func_array($handler, $arguments);
}
}
示例3: __construct
/**
* Create a new server application instance.
*
* @param string $path The path to the application files.
*/
public function __construct(string $path)
{
$this->path = $path;
// Register some routes.
$this->dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/', actions\IndexAction::class);
$r->addRoute('GET', '/blog', actions\BlogAction::class);
$r->addRoute('GET', '/category/{category}', actions\CategoryAction::class);
$r->addRoute('GET', '/portfolio', actions\PortfolioAction::class);
$r->addRoute('GET', '/{year:\\d{4}}/{month:\\d{2}}/{day:\\d{2}}/{name}', actions\ArticleAction::class);
$r->addRoute('GET', '/{asset:(?:content|assets)/[^?]+}[?{query}]', actions\AssetAction::class);
// Complicated feed routes :/
$r->addRoute('GET', '/sitemap.xml', actions\SitemapAction::class);
$r->addRoute('GET', '/feed[/{category:[a-z]+}]', actions\AtomFeedAction::class);
$r->addRoute('GET', '/feed.atom', actions\AtomFeedAction::class);
$r->addRoute('GET', '/feed/{category:[a-z]+}.atom', actions\AtomFeedAction::class);
$r->addRoute('GET', '/feed.rss', actions\RssFeedAction::class);
$r->addRoute('GET', '/feed/{category:[a-z]+}.rss', actions\RssFeedAction::class);
});
// Create the server object.
$this->server = new Server(new RequestHandler($this));
// Create some core services.
$this->renderer = new Renderer($path . '/templates');
$this->assetManager = new AssetManager($path . '/static');
$this->articleStore = new ArticleStore($path . '/articles');
}
示例4: setUp
public function setUp()
{
$router = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/', ['TestApp\\Controller\\Index', 'index']);
});
$this->app = new App($router);
}
示例5: setUp
public function setUp()
{
chdir(__DIR__ . '/app/');
$this->router = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/load', ['TestApp\\Controller\\Index', 'loadedParams'], ['name' => 'load']);
});
}
示例6: __invoke
/**
* Invocation
*
* Register routes container and request attributes
*
* @param ServerRequestInterface $request Request
* @param ResponseInterface $response Response
* @param TornadoHttp $next Next Middleware - TornadoHttp container
* @return ResponseInterface
* @throws HttpMethodNotAllowedException
* @throws HttpNotFoundException
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, TornadoHttp $next)
{
/** @var \FastRoute\Dispatcher\GroupCountBased $dispatcher */
$dispatcher = FastRoute\simpleDispatcher(function (RouteCollector $routeCollector) {
foreach ($this->routes as $key => $route) {
$routeCollector->addRoute($route['methods'], $route['path'], $key);
}
});
$method = $request->getMethod();
$uri = rawurldecode(parse_url($request->getUri(), \PHP_URL_PATH));
$route = $dispatcher->dispatch($method, $uri);
$handler = null;
$vars = null;
switch ($route[0]) {
case Dispatcher::NOT_FOUND:
throw new HttpNotFoundException('Inexistent route for the url ' . $request->getUri());
case Dispatcher::METHOD_NOT_ALLOWED:
throw new HttpMethodNotAllowedException('Method not allowed');
case Dispatcher::FOUND:
$handler = $route[1];
$vars = $route[2];
break;
}
$request = $request->withAttribute(Router::REGISTER_KEY, $handler);
foreach ($vars as $name => $value) {
$request = $request->withAttribute($name, $value);
}
return $next($request, $response);
}
示例7: setUp
public function setUp()
{
$config = Loader::load();
$config['router'] = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/', ['TestApp\\Controller\\IndexController', 'index']);
});
$this->app = new App(Container\PHPDiFactory::buildContainer($config));
}
示例8: __construct
public function __construct()
{
$this->dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/', 'IndexController');
$r->addRoute('POST', '/', 'IndexController');
});
$this->request = Request::createFromGlobals();
}
示例9: registerRoutes
/**
* Register necessary routes with route provider
*/
protected function registerRoutes()
{
$this->dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $routeCollector) {
$this->each(function (RouteRequest $routeRequest) use($routeCollector) {
$routeCollector->addRoute($routeRequest->method, $routeRequest->route, $routeRequest->callable);
});
});
}
示例10: createDispatcher
/**
* Instantiates the router using the routes in the container.
*/
private function createDispatcher()
{
$this->dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $collector) {
foreach ($this->container->get('routes') as $route) {
$collector->addRoute($route['method'], $route['pattern'], $route['service']);
}
});
}
示例11: setUp
public function setUp()
{
$this->router = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $manager) {
$manager->addRoute('GET', '/', ['TestApp\\Controller\\IndexController', 'index'], ['name' => 'index']);
$manager->addRoute('GET', '/fail', ['TestApp\\Controller\\IndexController', 'none'], ['name' => 'fail']);
$manager->addRoute('GET', '/dummy', ['TestApp\\Controller\\IndexController', 'dummy'], ['name' => 'dummy']);
});
}
示例12: dispatcher
/**
* @return Dispatcher
*/
protected function dispatcher()
{
return FastRoute\simpleDispatcher(function (RouteCollector $collector) {
foreach ($this->directory as $request => $action) {
list($method, $path) = explode(' ', $request, 2);
$collector->addRoute($method, $this->directory->prefix($path), $action);
}
});
}
示例13: setUp
public function setUp()
{
chdir(__DIR__ . '/app/');
$config = Loader::load();
$config['router'] = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/load', ['TestApp\\Controller\\IndexController', 'loadedParams'], ['name' => 'load']);
});
$this->container = Container\PHPDiFactory::buildContainer($config);
}
示例14: setUp
public function setUp()
{
$config = Loader::load();
$config['router'] = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/', [get_class($this), 'index']);
});
$config['dispatcher'] = \Di\object('PennyTest\\Utils\\FastSymfonyDispatcher')->constructor(\Di\get('router'), \Di\get('di'));
$this->app = new App(Container\PHPDiFactory::buildContainer($config));
}
示例15: setUp
public function setUp()
{
$this->router = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $router) {
$router->addRoute('GET', '/', ['TestApp\\Controller\\Index', 'diTest']);
});
$builder = new ContainerBuilder();
$builder->useAnnotations(true);
$dic = $builder->build();
$this->container = $dic;
}