Symfony 2 dynamic routing (e.g. for stores)

I'm new to Symfony 2, now I'm trying to get dynamic routing, I mean really dynamic.

For example:

example.com/en/categoryLevel1/categoryLevel2/categoryLevel3/productId-ProductName

      

OR

example.com/en/categoryLevel1/categoryLevel2/productId-ProductName

      

OR

example.com/en/categoryLevel1/categoryLevel2/categoryLevel3/

      

The number of category levels (category depth) should be flexible up to 100%

. It should be possible and be able to use one level up to twenty levels.

Where is the entry point for setting this parameter (which classes perform these routing functions)?

Another example:

routes on the old page:

example.com/ {categoryLvl1 }/ {{PRODUCTID}

the new page has some changes in the routes:

example.com/ {languages ​​}/ {catLevel1 }/ {catLevel2 }/.../ {PRODUCTNAME}

how i do regex etc i know. But I can't find a routing process in symfony (pre-routing process is better). I would like to create a pre-routing class and override the "normal" routing of symfony2. i have to match old and new, both are completely dynamic .. old is written in ZF1 (pretty easy for me) but symfony2 is a new area for me ...

+3


source to share


1 answer


Suppose you have a package that handles this type of url, you can add the following to the package routing.yml

(I prefer yml, YMMV).

YourSomethingBundle_main_any:
    pattern:  /{request}
    defaults: { _controller: YourSomethingBundle:Main:dispatcher }
    requirements:
        request: ".*"

      

Important: This is a "gimmick" to handle the actual request path in your controller. You must either prefix the path pattern

, or load this bundle after all other packages, or the other routes will no longer work.



According to the SF2 conventions, you will now have a class MainController

with a method dispatcherAction

:

<?php

namespace Your\SomethingBundle\Controller;

use \Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MainController extends Controller
{
    public function dispatcherAction($request='')
    {
        $request = preg_split('|/+|', trim($request, '/'));

        // ... and so on.
    }
}

      

+3


source







All Articles