简体   繁体   中英

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

I'm new at Symfony 2, now I'm trying to get a 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 (the category depth) have to be flexible to 100% . It must be possible and able to use one level to twenty levels.

Where is the entry point to setup this (which classes are doing those routing stuff)?

Another example is:

routes on the old page:

example.com/{categoryLvl1}/{categoryLvl2}/.../p-{productId}

at the new page are some changes in the routes:

example.com/{lang}/{catLevel1}/{catLevel2}/.../{productId}-{productName}

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

Let's assume you have a bundle that handles this type of URL, you might add the following in the bundle's routing.yml (I prefer yml, YMMV).

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

Important: This is a “catch-all”, letting you process the actual request path in your controller. You should either prefix the pattern path, or load this bundle after all other bundles, or other routes will no longer work.

As per SF2 conventions, you would now have a MainController class with a dispatcherAction method:

<?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.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM