2011-10-03 25 views
0

我在我的應用程序中重定向請求時遇到問題。如何將請求重定向到www.example.com/admin到Zend中的不同模塊

我的規則:

  • employer.domain.com - 應指向用人單位的頁面 - 使用默認模塊
  • employer.domain.com/panel/ - 應指向特定僱主的管理頁面 - 使用儀表板模塊
  • www.domain.com - s HOULD點聚集所有僱主頁 - 使用默認模塊

我已經測試了很多不同的路線,但是當一個路由的工作,其他的會出現問題。通常它只適用於根路徑,但是當我添加對某個控制器和操作的調用時 - 它崩潰了。也許我應該寫定製控制器插件?你怎麼看?

這是我目前的配置。這是一團糟,但也許它會幫助捕捉一些愚蠢的錯誤。

// employer.domain.com/panel 
$pathRoute_panel = new Zend_Controller_Router_Route(
    ':panel/:controller/:action/:id/', 
    array(
     'panel' => '', 
     'module' => 'dashboard', 
     'controller' => 'index', 
     'action' => 'index', 
     'id' => '', 
    ), 
    array(
     'panel' => 'panel' 
    ) 
); 

$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
    ':employer.'.$config['host'], 
    null, 
    array(
     'employer' => '([a-z0-9]+)', 
    ) 
); 
$router->addRoute('employer_panel', $subdomainRoute->chain($pathRoute_panel)); 


// employer.domain.com - main employer page 
$pathRoute_panel = new Zend_Controller_Router_Route(
    '', 
    array(
     'module' => 'default', 
     'controller' => 'vcard', 
     'action' => 'index', 
     'id' => '', 
    ) 
); 

$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
    ':employer.'.$config['host'], 
    null, 
    array(
     'employer' => '([a-z0-9]+)', 
    ) 
); 
$router->addRoute('employer_vcard', $subdomainRoute->chain($pathRoute_panel)); 


// domain.com/ 
$pathRoute = new Zend_Controller_Router_Route_Module(
    array(
     'module' => 'default', 
     'controller' => 'index', 
     'action' => 'index', 
    ), 
    $dispatcher, 
    $request 
); 

$route = new Zend_Controller_Router_Route_Hostname($config['host']); 
$router->addRoute('default', $route->chain($pathRoute)); 

// www.domain.com/ 
$pathRoute = new Zend_Controller_Router_Route_Module(
    array(
     'module' => 'default', 
     'controller' => 'index', 
     'action' => 'index', 
    ), 
    $dispatcher, 
    $request 
); 

$route = new Zend_Controller_Router_Route_Hostname('www.'.$config['host']); 
$router->addRoute('default_www', $route->chain($pathRoute)); 

編輯:這是我的解決方案:

// employer.domain.com 
$pathRoute_panel = new Zend_Controller_Router_Route_Module(
    array(
     'module' => 'default', 
     'controller' => 'vcard', 
     'action' => 'index', 
     'id' => '', 
    ) 
); 

$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
    ':employer.'.$config['host'], 
    null, 
    array(
     'employer' => '([a-z0-9]+)', 
    ) 
); 
$router->addRoute('employer_vcard', $subdomainRoute->chain($pathRoute_panel)); 

// employer.domain.com/panel/ 
$pathRoute_panel = new Zend_Controller_Router_Route(
    'panel/:controller/:action/:id/', 
    array(
     'panel' => 'panel', 
     'module' => 'dashboard', 
     'controller' => 'index', 
     'action' => 'index', 
     'id' => '', 
    ) 
); 

$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
    ':employer.'.$config['host'], 
    null, 
    array(
     'employer' => '([a-z0-9]+)', 
    ) 
); 
$router->addRoute('employer_panel', $subdomainRoute->chain($pathRoute_panel)); 
+0

隨着你張貼的路線,他們哪個按預期工作,哪些沒有?你能舉一個例子,當你添加控制器和動作時不起作用的URL。 –

+0

Works:employer.domain.com,employer.domain.com/panel/,domain.com 不工作:employer.domain.com/some-controller/some-action –

回答

0
// Enforce Subdomain usage 
// Will match employer.domain.com/* 
$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
    'employer.'.$config['host'] . '/*' 
); 


// Will match /panel/{id, required}/{controller, optional}/{action, optional}/{further parameters, optional} 
$pathRoute_panel = new Zend_Controller_Router_Route(
    'panel/:id/:controller/:action/*', 
    array(
     'module' => 'dashboard', 
     'controller' => 'index', 
     'action' => 'index' 
    ), 
    array(
     'id' => '\d+' 
    ) 
); 

// employer.domain.com - main employer page 
// will match everything not matched before! 
$pathRoute_page = new Zend_Controller_Router_Route(
    '*', 
    array(
     'module' => 'default', 
     'controller' => 'vcard', 
     'action' => 'index' 
    ) 
); 

// You can add several routes to one chain ;) The order does matter. 
$subDomainRoute->chain($pathRoute_page); 
$subDomainRoute->chain($pathRoute_panel); 
$router->addRoute($subDomainRoute); 

註釋是在代碼中。你可以使用通配符(*)來進一步匹配任何東西!沒有必要使用default_www路由 - 這只是默認行爲(默認路由現在將匹配每個子域,但是僱主,因爲它與subDomainRoute相匹配)。

+0

我現在正在嘗試您的解決方案。 PS:我知道不止一條路線可以被鏈接,但我太恐怖了;) –

+0

恐怕它不適合我。我重寫了我的應用程序的解決方案,並且無法重定向任何URL。下面我張貼我的解決方案,這似乎工作。 –

+0

@MaxBarnas:因爲你沒有執行特定的子域名,我想它會適用於任何子域名。 – Fge

相關問題