Phalcon subdomain routing

1.7k Views Asked by At

How to connect to the subdomains Phalcon:

city1.site.com
city2.site.com
city3.site.com
...
cityN.site.com

city - in the database

I am trying to do so

$router->add('{subdomain:\w+}.{domain:.+}', array(
    'controller' => 'category',
    'action' => 'categorySearch'
        )
);

but does not work.

2

There are 2 best solutions below

0
On

Phalcon's router doesn't match subdomains. You have to match $_SERVER['SERVER_NAME'] with a regular expression to create corresponding routers.

<?php
$di = new \Phalcon\Di\FactoryDefault();

$di->setShared('router', function() {

    // Match subdomain with regular expression
    if(preg_match("/^(\\w+)\\.site\\.com$/", $_SERVER['SERVER_NAME'], $matches) === 1) {
        $subdomain = $matches[1];
    }

    // Create a router without default routes
    $router = new \Phalcon\Mvc\Router(false);

    if (isset($subdomain)) {
        // Create routes for subdomains
        $router->add('/category', array(
            'controller' => 'category',
            'action' => 'categorySearch'
        ));
    } else {
        // Create routes for main domain
    }

    return $router;
});

// Retrieve corresponding router at runtime
$di->getShared('router')->handle();
0
On

may be this can help you problem

$di['router'] = function() 
{
    $router = new Phalcon\Mvc\Router(false);

    switch ($_SERVER['HTTP_HOST']) 
    {
        case 'm.domain.com':    
            $router->add('/m/xxx/yyy', array(
                'controller' => 'xxx',
                'action' => 'yyy'
            ));

            //...
            break;
        default:
            $router->add('/xxx/yyy', array(
                'controller' => 'xxx',
                'action' => 'yyy'
            )); 
            break;
    }

    return $router; 
};