以下是我Bootstrap
课程中定义的函数。我必须错过Zend进行路由和调度的方式。我想要完成的任务很简单:对于任何因任何原因无法发送的请求/foo/bar/*
,请尝试/index/foo/bar/
。我遇到的问题是当FooController
存在时,我得到Action "foo" does not exist
。基本上,isDispatchable
总是错误的。
public function run() {
$front = Zend_Controller_Front::getInstance();
$request = $front->getRequest();
$dispatcher = $front->getDispatcher();
//$controller = $dispatcher->getControllerClass($request);
if (!$dispatcher->isDispatchable($request)) {
$route = new Zend_Controller_Router_Route(
':action/*',
array('controller' => 'index')
);
$router = $front->getRouter();
$router->addRoute('FallBack', $route);
}
$front->dispatch();
}
答案 0 :(得分:0)
如果我理解你的想法是正确的
你会尝试使用__call
魔术方法吗?
然后使用$this->_redirect();
进行默认操作,例如
此处有更多信息http://php.net/manual/en/language.oop5.overloading.php
<强>更新强>
如果您在第480行打开Zend / Controller / Action.php
public function __call($methodName, $args)
{
require_once 'Zend/Controller/Action/Exception.php';
if ('Action' == substr($methodName, -6)) {
$action = substr($methodName, 0, strlen($methodName) - 6);
throw new Zend_Controller_Action_Exception(sprintf('Action "%s" does not exist and was not trapped in __call()', $action), 404);
}
throw new Zend_Controller_Action_Exception(sprintf('Method "%s" does not exist and was not trapped in __call()', $methodName), 500);
}
我打算做的是扩展这个类并完全覆盖__call
函数
classs My_Controller_Action extends Zend_Controller_Action{
public function __call($methodName, $args)
{
///// do your magic here ......redirection or logging the request or what ever
}
}
并确保您的控制器扩展新创建的类</ p>
class FooController extends My_Controller_Action
{
public function indexAction()
{
// action body
}
}
因此,如果您调用不存在的操作__call
的某些方式将会运行
这个想法只是关于不存在的行为
如果控制器不存在,它将无法工作
答案 1 :(得分:0)
所以这似乎有效,但不是最好的答案,因为它只是放弃了所有的参数。我可能会尝试很快在插件中转发/index/[original uri]
:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initRoute() {
$front = Zend_Controller_Front::getInstance();
$routes = array(
'FallBack' => new Zend_Controller_Router_Route(
':controller/:action/*',
array('controller' => 'index', 'action' => 'index')
)
);
$router = $front->getRouter();
$router->removeDefaultRoutes();
$router->addRoutes($routes);
$front->setRouter($router);
return $router;
}
protected function _initPlugin() {
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new My_Controller_Plugin_FallBack());
}
}
class My_Controller_Plugin_FallBack extends Zend_Controller_Plugin_Abstract {
public function preDispatch(Zend_Controller_Request_Abstract $request) {
$front = Zend_Controller_Front::getInstance();
$dispatcher = $front->getDispatcher();
$router = $front->getRouter();
if (($router->getCurrentRouteName() == 'FallBack') &&
!$dispatcher->isDispatchable($request)) {
$request->setActionName($request->getControllerName());
$request->setControllerName('index');
}
}
}