public function testPatternMatch() { $route1 = new Route\UrlRoute('foo'); $route2 = new Route\UrlRoute('bar'); $router = new Router(); $router->match('GET *', $route2, 3); $router->match('GET foo', $route1); $router->match('GET foo/**', $route2, 2); $match = $router->findMatch(['foo'], 'GET'); $this->assertInstanceOf('Jivoo\\Http\\Route\\UrlRoute', $match); $this->assertEquals('foo', $match->getUrl()); $router->match('bar/:baz', $route2); $match = $router->findMatch(['bar', 'foobar'], 'GET'); $this->assertInstanceOf('Jivoo\\Http\\Route\\UrlRoute', $match); $this->assertEquals('bar', $match->getUrl()); $this->assertEquals(['baz' => 'foobar'], $match->getParameters()); $match = $router->findMatch(['foo', 'baz', 'foobar'], 'GET'); $this->assertInstanceOf('Jivoo\\Http\\Route\\UrlRoute', $match); $this->assertEquals('bar', $match->getUrl()); $this->assertEquals(['baz', 'foobar'], $match->getParameters()); $router->match('bar/:0/*', $route1); $match = $router->findMatch(['bar', 'baz', 'foobar'], 'GET'); $this->assertInstanceOf('Jivoo\\Http\\Route\\UrlRoute', $match); $this->assertEquals('foo', $match->getUrl()); $this->assertEquals(['baz', 'foobar'], $match->getParameters()); $this->assertNull($router->findMatch(['foo'], 'POST')); $this->assertNull($router->findMatch(['baz', 'bar'], 'GET')); }
<?php use Jivoo\Http\ActionRequest; use Jivoo\Http\Route\CallableRoute; use Jivoo\Http\Route\CallableScheme; use Jivoo\Http\Router; use Jivoo\Http\SapiServer; use Psr\Http\Message\ResponseInterface; require '../vendor/autoload.php'; $router = new Router(); $router->addScheme(new CallableScheme()); $router->root(function (ActionRequest $request, ResponseInterface $response, $parameters) { $response->getBody()->write('Hello, World'); return $response; }); $router->error(function (ActionRequest $request, ResponseInterface $response, $parameters) { $response->getBody()->write('Page not found'); return $response; }); $router->match('foo', function (ActionRequest $request, ResponseInterface $response, $parameters) { $response->getBody()->write('Foo'); return $response; }); $router->match('foo/:bar', function (ActionRequest $request, ResponseInterface $response, $parameters) { $response->getBody()->write('Foo: ' . $parameters['bar']); return $response; }); $server = new SapiServer($router); $server->listen();