0

如何连接到子域 Phalcon:

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

城市 - 在数据库中

我正在尝试这样做

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

但不起作用。

4

2 回答 2

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();
于 2015-05-19T18:28:14.567 回答
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; 
};
于 2017-06-16T22:39:00.383 回答