function testGenerateAdvanced() { $route = new Route('/users/{id}/{action}', ''); $route->setRequirement('id', '\\d+'); $generator = new Generator(RequestContext::create()); $this->assertEquals('http://localhost/users/100/edit', $generator->generate($route, ['id' => 100, 'action' => 'edit'])); }
function testSchemes() { $routes = new RouteCollection(); $route = new Route('/foo/{id}/bar/{name}', ''); $route->setSchemes(['https']); $routes->add($route); $context = RequestContext::create(); $context->setScheme('https'); $this->assertEquals(['https'], $route->getSchemes()); $this->assertEquals('https', $context->getScheme()); $matcher = new Matcher($context); $this->assertEquals($route, $matcher->match('/foo/100/bar/steven', $routes)); $context->setScheme('http'); $this->setExpectedExceptionRegExp('Slince\\Routing\\Exception\\RouteNotFoundException'); $matcher->match('/foo/100/bar/steven', $routes); }
function testGenerate() { $context = RequestContext::create(); $context->setHost('m.domain.com'); $router = RouterFactory::create($context); $routeBuilder = $router->getRouteBuilder(); $routeBuilder->add('/path', ''); $routeBuilder->http('/users/{id}', ['name' => 'home.dash', 'action' => 'UsersController@dashboard'])->setRequirements(['id' => '\\d+', 'subdomain' => 'm'])->setHost('{subdomain}.domain.com')->setDefaults(['subdomain' => 'm']); $routeBuilder->prefix('user', function (RouteBuilder $routeBuilder) { $routeBuilder->http('/messages', 'MessagesController@index'); $routeBuilder->http('/messages/{id}', 'MessagesController@show'); $routeBuilder->prefix('me', function (RouteBuilder $routeBuilder) { $routeBuilder->http('/account', 'UsersController@me'); }); }); $routeBuilder->prefix('admin', function (RouteBuilder $routeBuilder) { $routeBuilder->http('/dashboard/', 'HomeController@index'); }); $this->assertEquals('http://www.domain.com/users/100', $router->generateByName('home.dash', ['subdomain' => 'www', 'id' => 100], true)); $this->assertEquals('http://m.domain.com/user/messages/100', $router->generateByAction('MessagesController@show', ['subdomain' => 'www', 'id' => 100], true)); $this->assertEquals('/user/me/account', $router->generateByAction('UsersController@me')); $this->assertEquals('/admin/dashboard', $router->generateByAction('HomeController@index')); }
/** * 开始处理请求 * @param Request $request * @return Response */ public function handleRequest(Request $request) { $this->setParameter('request', $request); //绑定Request到RequestContext $context = $this->bindRequestToContext($request, RequestContext::create()); $this->getRouter()->setContext($context); $route = $this->getRouter()->match($request->getPathInfo()); $this->setParameter('route', $route); //匹配出来的route存入核心缓存 //request匹配完毕,待派发 $this->dispatchEvent(EventStore::PROCESS_REQUEST, ['request' => $request, 'route' => $route]); $action = $route->getAction(); if (is_callable($action)) { $response = $this->runCallableAction($action, $route->getParameters()); } else { list($applicationName, $controller, $action) = explode('@', $action); $this->setParameter('application', $applicationName); $this->setParameter('controller', $controller); $this->setParameter('action', $action); $response = $this->runApplication($applicationName, $controller, $action, $route->getParameters()); } return $response; }
function testCreate() { $this->assertInstanceOf('Slince\\Routing\\Matcher', Factory::createMatcher()); $this->assertInstanceOf('Slince\\Routing\\Generator', Factory::createGenerator(RequestContext::create())); $this->assertInstanceOf('Slince\\Routing\\RouteCollection', Factory::createRoutes()); }