protected function configureRoutes(RouteCollection $collection) { $collection->remove('create'); $em = $this->get('doctrine.orm.entity_manager'); $posts = $em->getRepository('SiteFrontEndBundle:Site')->findAll(); return $this->return('SiteFrontEndBundle:news.html.twig'); }
public function testLoad() { $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface'); $pool = $this->getMock('Sonata\\AdminBundle\\Admin\\Pool', array(), array($container, 'title', 'logoTitle')); $adminPoolLoader = new AdminPoolLoader($pool, array('foo_admin', 'bar_admin'), $container); $routeCollection1 = new RouteCollection('base.Code.Route.foo', 'baseRouteNameFoo', 'baseRoutePatternFoo', 'baseControllerNameFoo'); $routeCollection2 = new RouteCollection('base.Code.Route.bar', 'baseRouteNameBar', 'baseRoutePatternBar', 'baseControllerNameBar'); $routeCollection1->add('foo'); $routeCollection2->add('bar'); $routeCollection2->add('baz'); $admin1 = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface'); $admin1->expects($this->once())->method('getRoutes')->will($this->returnValue($routeCollection1)); $admin2 = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface'); $admin2->expects($this->once())->method('getRoutes')->will($this->returnValue($routeCollection2)); $pool->expects($this->any())->method('getInstance')->will($this->returnCallback(function ($id) use($admin1, $admin2) { switch ($id) { case 'foo_admin': return $admin1; case 'bar_admin': return $admin2; } return; })); $collection = $adminPoolLoader->load('foo', 'sonata_admin'); $this->assertInstanceOf('Symfony\\Component\\Routing\\RouteCollection', $collection); $this->assertInstanceOf('Symfony\\Component\\Routing\\Route', $collection->get('baseRouteNameFoo_foo')); $this->assertInstanceOf('Symfony\\Component\\Routing\\Route', $collection->get('baseRouteNameBar_bar')); $this->assertInstanceOf('Symfony\\Component\\Routing\\Route', $collection->get('baseRouteNameBar_bar')); }
/** * @param $requestUrl * @param string $requestMethod * @return null * @throws \Exception */ public function match($requestUrl, $requestMethod = 'GET') { $isRegexp = false; foreach ($this->routes->all() as $route) { if (strpos($requestUrl, $route->getNamekey(), 0)) { throw new \Exception("Don't use route name key as part of your route"); } if (!in_array($requestMethod, (array) $route->getMethods())) { continue; } $url = $route->getUrl(); //bind name $name = $route->getName(); if (!empty($name)) { $this->namedroute[$name] = $route; } if (in_array($requestUrl, (array) $url)) { $route->dispatch(); return $route; } $isRegexp = $this->_PregMatch($url, $requestUrl, $route); if (!in_array($requestUrl, (array) $url) && $isRegexp == false) { continue; } $route->dispatch(); return $route; } return null; }
/** * Create a Router * * @return \mostofreddy\ruta\Router */ public function newInstance() { $o = new Router(); $collection = new RouteCollection(); $collection->routeFactory(new RouteFactory()); $o->routeCollection($collection); return $o; }
/** * Adds a route collection to the current set of routes (at the end of the current set). * * @param RouteCollection $collection A RouteCollection instance * @param string $prefix An optional prefix to add before each pattern of the route collection */ public function addCollection(RouteCollection $collection, $prefix = '') { $collection->addPrefix($prefix); foreach ($collection->getResources() as $resource) { $this->addResource($resource); } $this->routes = array_merge($this->routes, $collection->getRoutes()); }
/** * @param string $uri * * @return Route */ public function resolve($uri) { $uri = rtrim($uri, '/'); $uri = empty($uri) ? '/' : $uri; /** @var Route $route */ foreach ($this->routeCollection->all() as $route) { var_dump($route->getPattern(), $uri); if ($route->getPattern() === $uri) { return $route; } } throw new RouteNotFoundException(); }
/** * 创建一个router * * @param RequestContext $context * @return \Slince\Router\Router */ static function create(RequestContext $context = null) { if (is_null($context)) { $context = RequestContext::create(); } return new Router(RouteCollection::create(), new Matcher(), new Generator(), $context); }
/** * @param string $routeName уникальное имя маршрута. * @param array $params параметры. * @return string возвращает url адрес по имени маршрута и переданным параметрам. * @throws \InvalidArgumentException * @throws \LogicException */ public function createUrl($routeName, array $params = []) { $route = $this->routes->get($routeName); if (!$route) { throw new \InvalidArgumentException(sprintf('Route with the name "%s" does not exist', $routeName)); } $srcNames = $route->getParamNames(); $names = array_keys($params); if (array_diff($srcNames, $names)) { throw new \LogicException('Missing required params'); } $replacePairs = []; foreach ($params as $name => $value) { $replacePairs["{{$name}}"] = $value; } return strtr($route->getPattern(), $replacePairs); }
private function _bind() { foreach ($this->routes->all() as $route) { $name = $route->getName(); if (!empty($name)) { $this->namedroute[$name] = $route; } } }
/** * @param $requestUrl * @param string $requestMethod * @return mixed * @throws \Exception */ public function match($requestUrl, $requestMethod = 'GET') { $isRegexp = false; foreach ($this->routes->all() as $route) { $url = $route->getUrl(); if (in_array($requestUrl, (array) $url)) { $route->dispatch(); return $route; } if (!in_array($requestMethod, (array) $route->getMethods())) { continue; } $isRegexp = $this->_PregMatch($url, $requestUrl, $route); if (!in_array($requestUrl, (array) $url) && $isRegexp == false) { continue; } $route->dispatch(); return $route; } }
public function testGetRoutingTable() { $routeCollection = new RouteCollection(); $routeCollection->addRoute('GET', '/temperatures', function () { }); $routeCollection->addRoute('GET', '/temperatures/1', function () { }); $routeCollection->addRoute('POST', '/camera', function () { }); $this->assertCount(3, $routeCollection->getRoutingTable()); $this->assertEquals('/temperatures', $routeCollection->getRoutingTable()[0]['route']); $this->assertEquals('/temperatures/1', $routeCollection->getRoutingTable()[1]['route']); $this->assertEquals('/camera', $routeCollection->getRoutingTable()[2]['route']); }
/** * {@inheritdoc} */ public function getRouteCollection() { if (!$this->routes) { $this->routes = $this->controllers->getRoutes(); foreach ($this->aliases as $source => $alias) { $name = $source; $params = []; if ($query = substr(strstr($source, '?'), 1)) { $name = strstr($source, '?', true); parse_str($query, $params); } if ($route = $this->routes->get($name)) { $this->routes->add($source, new Route($alias[0], array_merge($route->getDefaults(), $params, ['_variables' => $route->compile()->getPathVariables()]))); } } } return $this->routes; }
public function testAddByConstructor() { $routes = [new Route('#/0#i', function () { }), new Route('#/1#i', function () { }), new Route('#/2#i', function () { })]; $collection = new RouteCollection($routes); $this->assertSame($routes[0], $collection->current()); $collection->next(); $this->assertSame($routes[1], $collection->current()); $collection->next(); $this->assertSame($routes[2], $collection->current()); }
/** * 添加一个子路由集合 * * @param string $prefix * @param RouteCollection $routes */ function addCollection($prefix, RouteCollection $collection) { $collection->setPreifx($this->_prefix . '/' . trim($prefix, '/')); $this->_collections[$prefix] = $collection; }
/** * 创建一个前缀 * * @param string $prefix * @param \Closure $callback */ function prefix($prefix, \Closure $callback) { $routes = RouteCollection::create(); $this->getRouteCollection()->addCollection($prefix, $routes); call_user_func($callback, $routes); }
* * By default, any parameter in the :routePattern has the requirement '[a-z0-9-]+' except :page and :id * which have '\d+' by default. * * Finally, you have to return the collection related to a subdomain: * * return array( * :subdomain => $routes, * [...] * ); * * As you see, every RouteCollection (or module) is related directly with a subdomain. This ables you to * create an admin module, for example: * * $blog = new RouteCollection('blog'); * // ... * * $admin = new RouteCollection('admin'); * // ... * * return array( * 'www' => $blog, * 'admin' => $admin * ); * * Now if you access towards the www subdomain the active module will be 'blog', otherwise in the admin * subdomain the 'admin' module will be active. */ $routes = new RouteCollection(); $routes->match('root', '/', 'index#index'); return array('www' => $routes);
<?php use App\Modules\Index\Controller\IndexPageController; /* |------------------------------------------------------------------------- | RouteCollection |------------------------------------------------------------------------- | | Objeto no qual serão definidas as rotas utilizadas na aplicação | */ $routes = new RouteCollection(); /* |------------------------------------------------------------------------- | Rota para o método GET |------------------------------------------------------------------------- | | Define o local aonde serão armazenados os templates que serão utilizados na aplicação | */ /* * ***************************************************************************** * HOME / INDEX * **************************************************************************** */ $routes->setGet('/', IndexPageController::class, 'index'); return $routes;
/** * @param RouteCollection $collection */ public function addCollection(RouteCollection $collection) { foreach ($collection->all() as $name => $route) { $this->add($name, $route); } }
public function testMethods() { $router = new RouteCollection(); $this->assertFalse($router->has("home")); $this->assertNull($router->get("home")); $this->assertSame(0, $router->count()); // add one $this->assertInstanceOf("SugiPHP\\Routing\\RouteCollection", $router->add("home", new Route("/"))); $this->assertSame(1, $router->count()); $this->assertTrue($router->has("home")); $this->assertInstanceOf("SugiPHP\\Routing\\Route", $router->get("home")); // change it $this->assertInstanceOf("SugiPHP\\Routing\\RouteCollection", $router->set("home", new Route("/foo"))); $this->assertSame(1, $router->count()); $this->assertTrue($router->has("home")); $this->assertInstanceOf("SugiPHP\\Routing\\Route", $router->get("home")); // remove it $this->assertInstanceOf("SugiPHP\\Routing\\RouteCollection", $router->delete("home")); $this->assertFalse($router->has("home")); $this->assertNull($router->get("home")); $this->assertSame(0, $router->count()); }
protected function configureRoutes(RouteCollection $collection) { $collection->remove('delete'); }
public function testAddRedirect() { $routes = new RouteCollection(); $routes->addRedirect('users', 'Users::index', 307); $expected = ['users' => '\\Users::index']; $this->assertEquals($expected, $routes->getRoutes()); $this->assertTrue($routes->isRedirect('users')); $this->assertEquals(307, $routes->getRedirectCode('users')); }
/** * @param Route $route * @return $this */ public function addRoute(Route $route) { $this->routeCollection->add($route); return $this; }