/** * Setup the route table. * * @return RouteCollection */ public function setup() : RouteCollection { $this->router = new RouteCollection(); $this->router->setStrategy(new UriStrategy()); $this->addRoute('GET', '/', new Index()); return $this->router; }
/** * @param array $configuration * * @throws NoRouteStrategyDefinedException */ public function __construct(array $configuration = []) { $this->configuration = new Configuration($configuration); $this->container = new Container($this->configuration['services']); $this->container->singleton(Configuration::class, $this->configuration); if (array_key_exists('route_strategy', $configuration) === false) { throw new NoRouteStrategyDefinedException(); } $routeStrategy = $configuration['route_strategy']; $this->router = new RouteCollection($this->container); $this->router->setStrategy(new $routeStrategy()); foreach ($this->configuration['routes'] as $collection) { foreach ($collection as $route) { $this->router->addRoute($route['method'], $route['pattern'], $route['controller']); } } }
/** * Use the register method to register items with the container via the * protected $this->container property or the `getContainer` method * from the ContainerAwareTrait. * * @return void */ public function register() { $this->container->singleton('router', function () { $router = new RouteCollection($this->container); $router->setStrategy(new UriStrategy()); return $router; }); $this->container->singleton('request', function () { return Request::createFromGlobals(); }); }
/** * @inheritdoc */ public function createServiceInstance(ServiceManagerInterface $serviceManager) { $router = new RouteCollection(); $router->setStrategy(new LeagueControllerStrategy($serviceManager)); $this->setupMainBootstrap($router); $this->setupRESTBoard($router); $this->setupRESTThread($router); $this->setupRESTThreadFeed($router); $this->setupRESTPost($router); $this->setupRESTThreadReply($router); return $router; }
/** * Register method,. */ public function register() { $this->container->add('routes.file', function () { return $this->container->get('paths.app') . 'routes.php'; }); // Bind a route collection to the container. $this->container->singleton(RouteCollection::class, function () { $routes = new RouteCollection($this->container); $routes->setStrategy(new UriStrategy()); return $routes; }); }
/** * Use the register method to register items with the container via the * protected $this->container property or the `getContainer` method * from the ContainerAwareTrait. * * @return void */ public function register() { $router = new RouteCollection($this->getContainer()); $strategy = new RestfulStrategy(); $this->getContainer()->add(StrategyInterface::class, $strategy); $router->setStrategy($strategy); require_once dirname(__DIR__) . "/Http/routes.php"; $router = apply_filters("wp-json.routes", $router); // share the dispatcher throughout the container $this->getContainer()->add(Dispatcher::class, $router->getDispatcher()); // share the route collection $this->getContainer()->add(RouteCollection::class, $router); }
/** * Use the register method to register items with the container via the * protected $this->container property or the `getContainer` method * from the ContainerAwareTrait. */ public function register() { $this->container->share(RouteCollection::class, function () { $strategy = new ParamStrategy(); $strategy->setContainer($this->container); $router = new RouteCollection($this->container); $router->setStrategy($strategy); return $router; }); $this->container->add('router', function () { return $this->container->get(RouteCollection::class); }); }
/** * Use the register method to register items with the container via the * protected $this->container property or the `getContainer` method * from the ContainerAwareTrait. */ public function register() { $this->container->share(RouteCollection::class, function () { $strategy = new ParamStrategy(); $strategy->setContainer($this->container); $routes = new RouteCollection($this->container); $routes->setStrategy($strategy); // Register routes $routes->get('/', DeployController::class . '::index'); $routes->get('/{task}', DeployController::class . '::index'); $routes->get('/run/{hash}/{command}', DeployController::class . '::run'); return $routes; }); }
public function testCustomRequestParameter() { $container = new Container(); $fakeController = $this->getMockBuilder('FakeController')->setMethods(array('display'))->getMock(); $fakeController->expects($this->any())->method('display')->will($this->returnValue(new Response())); $container->add('Controller', $fakeController); $routes = new RouteCollection($container); $routes->setStrategy(new ControllerDefinitionStrategy()); $middleware = new DefineControllerMiddleware($routes, 'customRequestParameter'); $routes->get('test/{name}', 'Controller::display'); $request = $middleware->prepare(Request::create('test/george')); $definition = $request->attributes->get('customRequestParameter'); $this->assertInstanceOf('Laasti\\Route\\ControllerDefinition', $definition); $this->assertInstanceOf('FakeController', $definition->getInstance()); $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Request', $request); $this->assertEquals('george', $definition->getArguments()['name']); }
<?php require __DIR__ . "/vendor/autoload.php"; use Icicle\Http\Message\RequestInterface; use Icicle\Http\Message\Response; use Icicle\Http\Server\Server; use Icicle\Loop; use Icicle\Socket\Client\ClientInterface; use League\Route\Http\Exception\MethodNotAllowedException; use League\Route\Http\Exception\NotFoundException; use League\Route\RouteCollection; use League\Route\Strategy\UriStrategy; $server = new Server(function (RequestInterface $request, ClientInterface $client) { $router = new RouteCollection(); $router->setStrategy(new UriStrategy()); require __DIR__ . "/routes.php"; $dispatcher = $router->getDispatcher(); try { $result = $dispatcher->dispatch($request->getMethod(), $request->getRequestTarget()); $status = 200; $content = $result->getContent(); } catch (NotFoundException $exception) { $status = 404; $content = "not found"; } catch (MethodNotAllowedException $exception) { $status = 405; $content = "method not allowed"; } $response = new Response($status); $response = $response->withHeader("Content-Type", "text/html"); (yield $response->getBody()->end($content));