如何将 www.example.com/admin 的请求重定向到 Zend 中的不同模块
我在应用程序中重定向请求时遇到问题。
我的规则:
- employer.domain.com - 应指向雇主的页面 - 使用默认模块
- employer.domain.com/panel/ - 应指向特定雇主的管理页面 - 使用仪表板模块
- www.domain.com - 应指向聚合所有雇主的页面 - 使用默认 > 模块
我已经测试了很多不同的路线,但是当一条路线正常工作时,其他路线就会损坏。通常它仅适用于根路径,但是当我添加对某些控制器和操作的调用时 - 它会崩溃。也许我应该编写自定义控制器插件?你怎么认为?
这是我当前的配置。虽然很混乱,但也许这有助于发现一些愚蠢的错误。
// 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));
I have a problem with redirecting requests in my application.
My rules:
- employer.domain.com - should point to a page of the employer - uses default module
- employer.domain.com/panel/ - should point to a administration page of specific employer - uses dashboard module
- www.domain.com - should point to page aggregating all employers - uses default module
I've tested a lot of different routes, but when one route is working, other get broken. Also often it works only for root paths, but when I add call to some controller and action - it crashes. Maybe I should write custom controller plugin? What do You think?
Here is my current configuration. It's a mess, but maybe it will help catching some silly mistake.
// 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));
EDIT: This is my solution:
// 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));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
注释在代码中。您可以使用通配符 (*) 来进一步匹配任何内容!不需要
default_www
路由 - 这只是默认行为(默认路由现在将匹配除雇主之外的每个子域,因为它与subDomainRoute
匹配)。Annotations are in the code. You can use the wildcard (*) to match anything further! There's no need of the
default_www
route - this's just the default behaviour (the default route now will match every subdomain but employer as it is matched by thesubDomainRoute
).