Alt-rock ninja
Alt-rock ninja

Reputation: 165

Symfony, dynamic routing

I have a symfony project with multiple skins/templates that have their own routes, does anyone have an idea for a correct setup?

The custom RouteLoader does the job--but the generated routes are getting cached, and as far as i understand, there is no way to prevent route caching.

Some suggestions are:

Anyone? I cant go with multiple projects really, the amount of skins will be around 20-30~.

The reason for this setup is because its a target of Content-as-a-Service .. service, multiple clients use the project as a platform, and their setting decides which templates gets used.

Upvotes: 2

Views: 1388

Answers (3)

Julien Bourdic
Julien Bourdic

Reputation: 1410

I think it's possible to load dynamically twig templates depending of your user by adding a listener on kernel requests.

I can give you a piece of code which, I hope, could help you :

        /**
         * On Kernel Request triggers the request to get the user config
         * then adds TWIG paths depending on user TemplateName
         */
        public function onKernelRequest(GetResponseEvent $event)
        {
            if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
                return;
            }
            //$userConfig = Retrieve your user config
            if (null === $userConfig->getTemplateConfig()->getTemplate()->getName()) 
            {
                throw new TemplateConfigNotFoundException(sprintf("Could not find TemplateConfig for %s", $userConfig->getName()));
            }

            $template = $userConfig->getTemplateConfig()->getTemplate()->getName();

            $path = sprintf('%s/../../%s/Resources/views', __DIR__, ucfirst($template));

            if (!is_dir($path)) {
                throw new TemplateNotFoundException(sprintf("Could not find template %s", $template));
            }

            $this->loader->prependPath($path);
            $this->loader->addPath(sprintf('%s/../Resources/views/Default', __DIR__));
       }

With $this->loader defined as \Twig_Loader_Filesystem in your Listener constructor

Hope it can give you a clue

Upvotes: 1

Cerad
Cerad

Reputation: 48865

It sounds like you want to dynamically load bundles based on the host name? Not going to happen with Symfony 2 because of the caching. Especially the services.

Your best bet is to setup an app for each skin and then do some url majic to execute the desired app.php file. Clearly since you have defined a bundle for each skin then there is a finite number so having multiple apps should not be much or a burden.

It's possible that you might be able to work around the template issue. You would still need to load all your skin bundles but you could futz around with the template names or paths and probably get something to work.

But services? Unless you start appending host names to service id's then I don't see any work around.

Upvotes: 1

mblaettermann
mblaettermann

Reputation: 1943

Symfony2 already supports host aware routing out-of-the-box, like this:

website_customer_1:
    path:     /
    host:     customer1.example.com
    defaults: { _controller: Customer1Bundle:Main:startPage, theme: template1 }

website_customer_2:
    path:     /
    host:     customer2.example.com
    defaults: { _controller: Customer1Bundle:Main:startPage, theme: template2 }

Upvotes: 0

Related Questions