public function testRedirectName() { $root = self::$dm->find(null, self::ROUTE_ROOT); $redirect = new RedirectRoute(); $redirect->setPosition($root, 'redirectName'); $redirect->setRouteName('symfony_route'); $redirect->setParameters(array('param' => 7)); // parameters should be ignored in this case self::$dm->persist($redirect); self::$dm->flush(); self::$dm->clear(); $redirect = self::$dm->find(null, self::ROUTE_ROOT . '/redirectName'); $response = self::$controller->redirectAction($redirect); $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $response); $this->assertSame(302, $response->getStatusCode()); $this->assertSame('http://localhost/symfony_route_test?param=7', $response->getTargetUrl()); }
/** * Load routing data into the document manager. * * NOTE: We demo all possibilities. Of course, you should try to be * consistent in what you use and only use different things for special * cases. * * @param $dm */ public function load(ObjectManager $dm) { $session = $dm->getPhpcrSession(); $basepath = $this->container->getParameter('symfony_cmf_routing_extra.routing_repositoryroot'); $content_path = $this->container->getParameter('symfony_cmf_content.static_basepath'); if ($session->itemExists($basepath)) { $session->removeItem($basepath); } NodeHelper::createPath($session, dirname($basepath)); $root = $dm->find(null, dirname($basepath)); $locales = array('en', 'fr', 'de'); //TODO: can we get this from phpcr-odm in a sane way? $parent = new MultilangLanguageSelectRoute(); $parent->setPosition($root, basename($basepath)); $dm->persist($parent); foreach ($locales as $locale) { $home = new Route(); $home->setPosition($parent, $locale); $home->setDefault('_locale', $locale); $home->setRouteContent($dm->find(null, "{$content_path}/home")); $dm->persist($home); $company = new Route(); $company->setPosition($home, 'company'); $company->setDefault('_locale', $locale); $company->setRouteContent($dm->find(null, "{$content_path}/company")); $dm->persist($company); $team = new Route(); $team->setPosition($company, 'team'); $team->setDefault('_locale', $locale); $team->setRouteContent($dm->find(null, "{$content_path}/team")); $dm->persist($team); $more = new Route(); $more->setPosition($company, 'more'); $more->setDefault('_locale', $locale); $more->setRouteContent($dm->find(null, "{$content_path}/more")); $dm->persist($more); $projects = new Route(); $projects->setPosition($home, 'projects'); $projects->setDefault('_locale', $locale); $projects->setRouteContent($dm->find(null, "{$content_path}/projects")); $dm->persist($projects); $cmf = new Route(); $cmf->setPosition($projects, 'cmf'); $cmf->setDefault('_locale', $locale); $cmf->setRouteContent($dm->find(null, "{$content_path}/cmf")); $dm->persist($cmf); } // demo features of routing // we can create routes without locales, but will lose the language context of course $demo = new Route(); $demo->setPosition($parent, 'demo'); $demo->setRouteContent($dm->find(null, "{$content_path}/demo")); $demo->setDefault(RouteObjectInterface::TEMPLATE_NAME, 'SandboxMainBundle:Demo:template_explicit.html.twig'); $dm->persist($demo); // explicit template $template = new Route(); $template->setPosition($demo, 'atemplate'); $template->setRouteContent($dm->find(null, "{$content_path}/demo_template")); $template->setDefault(RouteObjectInterface::TEMPLATE_NAME, 'SandboxMainBundle:Demo:template_explicit.html.twig'); $dm->persist($template); // explicit controller $controller = new Route(); $controller->setPosition($demo, 'controller'); $controller->setRouteContent($dm->find(null, "{$content_path}/demo_controller")); $controller->setDefault('_controller', 'sandbox_main.controller:specialAction'); $dm->persist($controller); // alias to controller mapping $alias = new Route(); $alias->setPosition($demo, 'alias'); $alias->setRouteContent($dm->find(null, "{$content_path}/demo_alias")); $alias->setDefault(RouteObjectInterface::CONTROLLER_ALIAS, 'demo_alias'); $dm->persist($alias); // class to controller mapping $class = new Route(); $class->setPosition($demo, 'class'); $class->setRouteContent($dm->find(null, "{$content_path}/demo_class")); $dm->persist($class); // redirections // redirect to uri $redirect = new RedirectRoute(); $redirect->setPosition($parent, 'external'); $redirect->setUri('http://cmf.symfony.com'); $dm->persist($redirect); // redirect to other doctrine route $redirectRoute = new RedirectRoute(); $redirectRoute->setPosition($parent, 'short'); $redirectRoute->setRouteTarget($cmf); $dm->persist($redirectRoute); // redirect to Symfony route $redirectS = new RedirectRoute(); $redirectS->setPosition($parent, 'short1'); $redirectS->setRouteName('test'); $dm->persist($redirectS); // class to template mapping is used for all the rest $dm->flush(); }
/** * @expectedException LogicException */ public function testSetContent() { $content = $this->getMock('Symfony\\Cmf\\Component\\Routing\\RouteAwareInterface'); $redirect = new RedirectRoute(); $redirect->setRouteContent($content); }
public function testEnhanceControllerByClass() { // put a redirect route $root = self::$dm->find(null, self::ROUTE_ROOT); $route = new RedirectRoute(); $route->setRouteTarget($root); $route->setPosition($root, 'redirect'); self::$dm->persist($route); self::$dm->flush(); $expected = array('_controller' => 'symfony_cmf_routing_extra.redirect_controller:redirectAction', RouteObjectInterface::ROUTE_NAME => '/test/routing/redirect'); $request = Request::create('/redirect'); $matches = self::$router->matchRequest($request); ksort($matches); $this->assertTrue($request->attributes->has(DynamicRouter::ROUTE_KEY)); $this->assertEquals($expected, $matches); }