Zend路由 - 使用子域/域作为控制器

时间:2014-09-18 16:01:48

标签: php zend-framework routing subdomain

目前我已经定义并运行了这样的路线:

    $route = new Zend_Controller_Router_Route
    (
        ':token/:place/:controller/:action/*',
        array
        (
            'module'         => 'admin',
            'controller'     => 'public',
            'action'         => 'list',
            'token'          => 'default_company',
            'place'          => 'default_place'
        ),
        array
        (
            'token'  => '[a-z_]+',
            'place'  => '[a-z_]+'
        )
    );
    $router->addRoute('admin', $route);

因此,应用中的网址可能是myserver.com/google/europe/public/list

客户需要两个额外的选项才能到达同一页面: google.myserver.com/europe和google.com/europe

将处理所有DNS并配置Apache。目前我必须创建模式来处理这些路由,但我不知道如何提取子域和/或域作为参数并在引导中隐藏控制器/动作(以某种方式使它们默认)。也许之前已经做过并可以提供帮助?

我为google.myserver.com/europe创建了一个类似的模式,但它似乎无法解决问题:

    $route = new Zend_Controller_Router_Route
    (
        ':token.*.*/:place/*',
        array
        (
            'module'              => 'admin',
            'controller'          => 'public',
            'action'              => 'list',
            'token'               => '',
            'place'               => ''
        ),
        array
        (
            'token'  => '[a-z_]+',
            'place'  => '[a-z_]+'
        )
    );
    $router->addRoute('subdomain_route', $route);

感谢任何迹象表明我做错了什么。

1 个答案:

答案 0 :(得分:0)

我找到了答案,只要有人需要就分享。我已经在控制器init()中创建了一个帮助器来获取organization_token和place_path。它几乎解析了URL的不同情况并返回参数:

public static function getOrganizationTokenAndPlacePath($token, $path)
{           
    $url = explode('.', $_SERVER['HTTP_HOST']);
    $params = explode('/', $_SERVER['REQUEST_URI']);
    $subdomain = $url[0];
    $domain = $url[1];        
    $parsed = array();
    if (($token == 'default_token' || $path == 'default_place') && $domain == DEFAULT_APP_DOMAIN){                                   
        // Case http://organization_token.domain.com/place_path/
        $parsed['token'] = $subdomain;
        $parsed['path'] = $params[1];
        //die('Case1');
    }
    else if(($token == 'default_token' || $path == 'default_place') && $domain != DEFAULT_APP_DOMAIN){                       
        // Case http://some_subdomain.organization_token.com/place_path/
        $parsed['token'] = $domain;
        $parsed['path'] = $params[1];     

        // If place_path is not found, try to get the last entered place_path from the database
        if ($parsed['path'] == ''){
            $organization_model = new Admin_Model_Organization();
            $organization = $organization_model->getOrganizationByToken($parsed['token']);
            $places = $organization_model->getOrganizationPlaces($organization['organization_id']);
            if (sizeof($places) > 0){
                $last_added_place = array_pop($places);
                $parsed['path'] = $last_added_place['place_path'];
            }
        }

        //die('Case2');
    }
    else if ($domain != DEFAULT_APP_DOMAIN && $subdomain == 'www'){            
        // Case http://www.organization_token.com/some_param/place_path/
        $parsed['token'] = $domain;
        $parsed['path'] = $params[2];
        //die('Case3');
    }           
    else{
        // Case http://subdomain.domain.com/organization_token/place_path/
        $parsed['token'] = $token;
        $parsed['path'] = $path;     
        //die('Case4');
    }

    return $parsed;
}

希望这有助于某人