2011-10-03 110 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

相关问题