我建议您通过配置资源管理器、引导程序或前端控制器插件创建静态或动态路由:
在 Bootstrap.php 中定义静态路由的示例:
public function _initRoutes()
{
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter(); // default Zend MVC routing will be preserved
// create first route that will point from nonexistent action in mobile module to existing action in default module
$route = new Zend_Controller_Router_Route_Static(
'mobile/some-controller/some-action', // specify url to controller and action that dont exist in "mobile" module
array(
'module' => 'default', // redirect to "default" module
'controller' => 'some-controller',
'action' => 'some-action', // this action exists in "some-controller" in "default" module
)
);
$router->addRoute('mobile-redirect-1', $route); // first param is the name of route, not url, this allows you to override existing routes like default route
// repeat process for another route
}
这将有效地将/mobile/some-controller/some-action 的请求路由到/default/some-controller/some-action
some-controller和some-action应替换为正确的控制器和操作名称。
我使用的是静态路由,如果您路由到确切的 url,这没问题,但是由于大多数应用程序在 url 中使用额外的参数来使用控制器操作,所以最好使用动态路由。
在上面的示例中,只需将路由创建类更改为Zend_Controller_Router_Route
并将路由 url 更改为"mobile/some-controller/some-action/*"
,每个请求都将被动态路由,如下例所示:
/mobile/some-contoller/some-action/param1/55/param2/66
will point to
/default/some-controller/some-action/param1/55/param2/66
有关 ZF1 中路由的更多信息,请查看此链接