2014-09-18 60 views
1

和工作:Zend的路由 - 使用子域名/域控制器目前,我有这样的路线定义

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

因此,在应用程序的URL可以是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); 

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

+0

没有人知道??? – pakalbekim 2014-09-21 11:11:02

回答

0

我已经找到了答案,只是要份额,如果有人需要它。我创建了一个帮助程序,用于控制器init()以获取organization_token和place_path。它几乎解析URL的不同情况,并返回PARAMATERS:

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

希望这有助于有人