我有两个模块(默认和移动)移动模块是jquery-mobile中重写的默认门户,但控制器和操作要少得多!我想写一个控制器插件,检查移动模块中是否存在控制器和操作,如果不存在,我想将移动模块覆盖为默认值。我试试这个:
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
$dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
if ($request->getModuleName() == 'mobile') {
if (!$dispatcher->isDispatchable($request)) {
// Controller or action not exists
$request->setModuleName('default');
}
}
return $request;
}
但是$dispatcher->isDispatchable($request)
总是返回true
,尽管该操作不存在!:S并且我接收到"动作foo
不存在并且没有被困在__call()中"
我该怎么办?感谢
你有没有想过如何从应用程序的任何一端检查zend FM中是否存在控制器/操作?这是代码
$front = Zend_Controller_Front::getInstance();
$dispatcher = $front->getDispatcher();
$test = new Zend_Controller_Request_Http();
$test->setParams(array(
'action' => 'index',
'controller' => 'content',
)
);
if($dispatcher->isDispatchable($test)) {
echo "yes-its a controller";
//$this->_forward('about-us', 'content'); // Do whatever you want
} else {
echo "NO- its not a Controller";
}
编辑
像这样检查
$classMethods = get_class_methods($className);
if(!in_array("__call", $classMethods) &&
!in_array($this->getActionMethod($request), $classMethods))
return false;
还请参阅详细链接
我建议您通过配置资源管理器、引导程序或前端控制器插件进行静态或动态路由:
Bootstrap.hp:中定义静态路由的示例
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-concontroller/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中路由的更多信息,请查看此链接