Phalcon子域路由

时间:2015-05-19 08:51:02

标签: php phalcon phalcon-routing

如何连接子域Phalcon:

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

city - 在数据库中

我正在尝试这样做

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

但不起作用。

2 个答案:

答案 0 :(得分:4)

Phalcon的路由器与子域不匹配。您必须将$ _SERVER ['SERVER_NAME']与正则表达式匹配才能创建相应的路由器。

<?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();

答案 1 :(得分:0)

可能这可以帮助你解决问题

$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; 
};