Symfony2 routing for controllers using query parameters

I currently live in Bizzaro World with one client.

Since the symfony application we are writing has a response entered on a page in another application, we are forced to have only one URL for the application, but, fortunately, we get the chain passed to us.

So I need to do the exact opposite of how symfony does something, a kind of old MVC approach. I need routing to work through request parameters, routing to the right controller and rendering the correct answer, rather than using a standard and reasonable path.

So the URLs will be http://www.bizzaro.com/appname?route=%2fblog , not http://www.bizzaro.com/appname/blog , etc.

The routing section has good examples of how to ensure that periods will be executed as parameters for controller actions, but not for this rather regressive approach.

Where can i start?

I implemented solution 2 proposed by Corentin Dandoy below, but slightly modified to prevent a loop when searching for a root.

 namespace AppBundle\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class FrontControllerControllerController extends Controller { /** * Forward the request to the appropriate controller * @param Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function indexAction(Request $request) { // get the parameter that specifies the route to the 'real' homepage controller $homeroute = $this->container->getParameter('homeroute'); $route = $request->query->get('route'); // Convert the query-string route into a valid path route $path = '/'.$route; // if it is the route, then use the 'real' homepage controller, otherwise you end up in a routing loop! if ($path === '/') { $match = $this->get('router')->match('/' . $homeroute); } else { try { $match = $this->get('router')->match($path); } catch (ResourceNotFoundException $e) { throw $this->createNotFoundException('The route does not exist'); } } $params = array( 'request' => $request, ); return $this->forward($match['_controller'], $params); } } 
+5
source share
1 answer

SOLUTION 1

One simple solution (not scalable):

 namespace AppBundle\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class MainController { /** * @Route("/app", name="bizarro_app") */ public function mainAction(Request $request) { $route = $request->query->get('route'); switch ($route) { case 'blog': return $this->blogAction($request); default: throw $this->createNotFoundException('The route does not exist'); } } protected function blogAction(Request $request) { // Your blog page here return new Response('...'); } } 

SOLUTION 2

If you are not opposed to having two types of routes available, you can try the following:

 namespace AppBundle\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class MainController { /** * @Route("/app", name="bizarro_app") */ public function mainAction(Request $request) { $route = $request->query->get('route'); // Convert the query-string route into a valid path $path = '/'.$route; try { $match = $this->get('router')->match($path); } catch (ResourceNotFoundException $e) { throw $this->createNotFoundException('The route does not exist'); } $params = array( 'request' => $request, ); return $this->forward($match['_controller'], $params); } /** * @Route("/blog", name="bizarro_blog") */ public function blogAction(Request $request) { // Your blog page here return new Response('...'); } } 

This way you benefit from the Sf2 routing component. Keep in mind that I myself have not tested it.

+4
source

All Articles