/** * Initialize a Router. * * Routes require a 'path' key and may have optional 'methods' and 'defaults' keys. * * @param array $routeData Array of routes */ public function __construct(array $routeData) { $this->dispatcher = simpleDispatcher(function ($r) use($routeData) { foreach ($routeData as $data) { if (is_array($data)) { $data = Route::fromArray($data); } $r->addRoute($data->getMethods(), $data->getPath(), $data); } }); }
public function testConstraints() { // a route that matches a numeric argument only $r = new Route('/{gamma}', 'alpha/beta'); $r->constrain('gamma', '[0-9]+'); $this->assertFalse($r->match('/delta')); $this->assertTrue($r->match('/1234')); // a route that matches a partial string $r = new Route('/{gamma}', 'alpha/beta'); $r->constrain('gamma', '.+elt.+'); $this->assertFalse($r->match('/dellta')); $this->assertTrue($r->match('/delta')); // a route with multiple constraints $r = new Route('/{gamma}/import/{epsilon}', 'alpha/beta'); $r->constrain('gamma', '[0-9]+')->constrain('epsilon', 'mu|nu'); $this->assertFalse($r->match('/alpha/import/mu')); $this->assertFalse($r->match('/alpha/12/nu')); $this->assertFalse($r->match('/12/beta/nu')); $this->assertTrue($r->match('/12/import/nu')); $this->assertTrue($r->match('/0/import/mu')); }
$container['router'] = function ($c) { $app_path = $c['app_path']; $routes = $c['routes']; return new \pew\router\Router($routes); }; $container['routes'] = function ($c) { $app_folder = $c['app_path']; $routes_path = $app_folder . DIRECTORY_SEPARATOR . $c['config_folder'] . DIRECTORY_SEPARATOR . 'routes.php'; $definitions = (require $routes_path); $routes = []; foreach ($definitions as $path => $handler) { if (is_a($handler, \pew\router\Route::class)) { $routes[] = $handler; } elseif (is_string($handler) || is_callable($handler)) { // convert simple route to array route $handler = \pew\router\Route::from($path)->handler($handler)->methods('GET', 'POST'); $routes[] = $handler; } elseif (is_array($handler) && isset($handler['resource'])) { // create CRUD routes from resource route $controller_class = $handler['resource']; $slug = \Stringy\Stringy::create($controller_class)->humanize()->slugify(); $underscored = $slug->underscored(); $routes[] = ['path' => "/{$slug}/{id}/edit", 'handler' => "{$controller_class}@edit", 'methods' => 'GET POST', 'name' => "{$underscored}_edit", 'defaults' => $handler['defaults'] ?? [], 'conditions' => $handler['conditions'] ?? []]; $routes[] = ['path' => "/{$slug}/{id}/delete", 'handler' => "{$controller_class}@delete", 'methods' => 'GET POST', 'name' => "{$underscored}_delete", 'defaults' => $handler['defaults'] ?? [], 'conditions' => $handler['conditions'] ?? []]; $routes[] = ['path' => "/{$slug}/add", 'handler' => "{$controller_class}@add", 'methods' => 'GET POST', 'name' => "{$underscored}_add", 'defaults' => $handler['defaults'] ?? [], 'conditions' => $handler['conditions'] ?? []]; $routes[] = ['path' => "/{$slug}/{id}", 'handler' => "{$controller_class}@view", 'name' => "{$underscored}_view", 'defaults' => $handler['defaults'] ?? [], 'conditions' => $handler['conditions'] ?? []]; $routes[] = ['path' => "/{$slug}", 'handler' => "{$controller_class}@index", 'name' => "{$underscored}_index", 'defaults' => $handler['defaults'] ?? [], 'conditions' => $handler['conditions'] ?? []]; } elseif (isset($handler['handler'], $handler['path'])) { $routes[] = $handler; } else { throw new \InvalidArgumentException("Invalid route: missing path or handler");
public function testRouteUriAndDestination() { $_SERVER['PATH_INFO'] = '/project/22'; $route1 = new Route('project/22', 'projects/details'); $route1->match('project/22'); $request = new Request(new Env(), $route1); $this->assertEquals('/project/22', $request->uri()); $this->assertEquals('projects/details', $request->destination()); $_SERVER['PATH_INFO'] = '/login'; $route2 = new Route('login', 'users/login'); $route2->match('login'); $request = new Request(new Env(), $route2); $this->assertEquals('/login', $request->uri()); $this->assertEquals('users/login', $request->destination()); $_SERVER['PATH_INFO'] = '/'; $route3 = new Route('/', 'projects/index'); $route3->match('/'); $request = new Request(new Env(), $route3); $this->assertEquals('/', $request->uri()); $this->assertEquals('projects/index', $request->destination()); }