1

using symfony 2.8, i'm working with subdomains and i want to show different (lets say)home pages depending on the subdomain, i'm storing the subdomains in Domain table with a column named subdomain. ideally when the user visits sub.example.com i want to search the database for 'sub' and get the id of that row and set that as a global parameter for that specific domain, so that i can load the websitesettings and load other dynamic data from the database (using domain_id as the key)

this is what i presume to be correct, if there are better methods to deal with this same problem, please let me know, i might get a friend to give out a bounty if its new to me.

2 Answers 2

0

I suggest you listen to the kernel.controller event. Make sure your listener is container aware so that you can set the parameter by doing $this->container->setParameter('subdomain', $subdomain);

At this point you just need to check the parameter you set where it suits you, for example in your controller action so that you can return, for example, different views according to the current subdomain.

Reference:

  1. Container aware dispatcher
  2. Symfony2 framework events
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, but is it really ok to refer to the subdomain parameter everytime i need to make a db request? currently where i stand, i'll need to use domain_id in every single request to the database
That's about your domain logic so I think it depends really. It's a question kinda of too broad and more concerning Software Design I guess.
hmm yes :( anyhue i shall tryout your answer and let you know
do you by chance know of any techniques where i dont have to manually set the parameter in doctrine, but some thing like a configuration for doctrine where it always searches (internally) for rows with domain_id = 1, if i search for only first name it must return rows where domain_id = 1 and the firstname, i hope i was clear in my words.
I don't know any but you can always create a separate question for that. Otherwise you can create your own Adapter (see sitepoint.com/practical-aspects-of-the-adapter-pattern) and use that instead of using Doctrine directly. Your adapter can therefore add all requirements via the query builder for example. Or maybe a repository parent class? stackoverflow.com/questions/8146461/…
0

Have a look at my implementation, using a YAML configuration instead of a database: https://github.com/fourlabsldn/HostsBundle. You might be able to get some inspiration.

<?php
namespace FourLabs\HostsBundle\Service;

use Symfony\Component\HttpFoundation\RequestStack;
use FourLabs\HostsBundle\Model\DomainRepository;
use FourLabs\HostsBundle\Exception\NotConfiguredException;

abstract class AbstractProvider
{
    /**
     * @var RequestStack
     */
    protected $requestStack;

    /**
     * @var DomainRepository
     */
    protected $domainRepository;

    /**
     * @var boolean
     */
    protected $requestActive;

    public function __construct(RequestStack $requestStack, DomainRepository $domainRepository, $requestActive)
    {
        $this->requestStack = $requestStack;
        $this->domainRepository = $domainRepository;
        $this->requestActive = $requestActive;
    }

    protected function getDomainConfig()
    {
        $request = $this->requestStack->getCurrentRequest();

        if(is_null($request) || !$this->requestActive) {
            return;
        }

        $host = parse_url($request->getUri())['host'];

        if(!($domain = $this->domainRepository->findByHost($host))) {
            throw new NotConfiguredException('Domain configuration for '.$host.' missing');
        }

        return $domain;
    }
}

and the listener

<?php

namespace FourLabs\HostsBundle\EventListener;

use FourLabs\HostsBundle\Service\LocaleProvider;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class LocaleListener
{
    /**
     * @var LocaleProvider
     */
    private $localeProvider;

    public function __construct(LocaleProvider $localeProvider) {
        $this->localeProvider = $localeProvider;
    }

    public function onKernelRequest(GetResponseEvent $event) {
        if(HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
            return;
        }

        $event->getRequest()->setLocale($this->localeProvider->getLocale());
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.