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 Answers2

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
Francesco Casula
  • 26,184
  • 15
  • 132
  • 131
  • 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 – pinch boi triggered af Jan 04 '16 at 10:25
  • 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. – Francesco Casula Jan 04 '16 at 10:31
  • hmm yes :( anyhue i shall tryout your answer and let you know – pinch boi triggered af Jan 04 '16 at 10:34
  • 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. – pinch boi triggered af Jan 04 '16 at 13:14
  • I don't know any but you can always create a separate question for that. Otherwise you can create your own Adapter (see http://www.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? http://stackoverflow.com/questions/8146461/custom-repository-class-in-symfony2 – Francesco Casula Jan 04 '16 at 13:40
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());
    }
}
Jonny
  • 2,223
  • 23
  • 30