1

ZF2 user registration and creating a subdomain

 2 years ago
source link: https://www.codesd.com/item/zf2-user-registration-and-creating-a-subdomain.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

ZF2 user registration and creating a subdomain

advertisements

Im using the ZF 2 Framework and have the follow scenario.

  1. Company can register
  2. After Register it creates a subdomain (need changes in the .htaccess)

Example :

  • site.com => Frontend
  • company_a.site.com => Frontend for Company (company_id=1)
  • compaby_b.site.com => Frontend for Company (company_id=2)

What i want is that when a Users goes to company_a.site.com or company_b.site.com, that the Route is the same in the Application but the Data from DB depens on the subdomain name.

How i think it must work :

  1. User comes to company_a.site.com
  2. In the Main Controller i will make a Query to DB to see if exists a company with name "company_a" and if is so then assign a global variable with the company ID from the DB. So i can based on that ID load the content for the frontend.

I done something similar in my current project. I used a service to get the site details from a MySQL database using doctrine based on the domain name.

My service is

namespace Application\Service;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Application\Entity\Sites;

class SiteFactory implements FactoryInterface
{

    /**
     * @var EntityManager
     */
    protected $entityManager = NULL;

    /**
     *
     * @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator
     * @return \Application\Entity\Sites
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $request = $serviceLocator->get('Request');
        $site = new Sites();

        if (method_exists($request, 'getUri')) {
            $domainName = ltrim($request->getUri()->getHost(), 'www.');
            $entityManager = $serviceLocator->get('entityManager');
            $repository = $entityManager->getRepository('Application\Entity\Sites');
            $site = $repository->findOneByDomainName($domainName);
        }

        return $site;
    }

}

In my module.config I have

'service_manager' => array(
    'factories' => array(
        'site' => 'Application\Service\SiteFactory',
    ),
),

and to get the site in my controller I use

$this->getServiceLocator()->get('site');

to return the sites doctrine entity or an empty entity if no site found.

I hope this points you in the right direction.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK