找不到控制器时,通过默认控制器路由zend请求
问题描述:
下面是我的Bootstrap
类中定义的一个函数。我必须错过Zend进行路由和调度的方式。我正在努力完成的工作很简单:对于因任何原因无法分派的任何请求/foo/bar/*
请尝试/index/foo/bar/
。我遇到的问题是当FooController
存在时,我得到Action "foo" does not exist
。基本上,isDispatchable
总是错误的。找不到控制器时,通过默认控制器路由zend请求
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();
}
答
如果我理解你的想法正确 会尝试使用__call
魔术方法? 然后用$this->_redirect();
到您的默认动作例如
更多信息在这里http://php.net/manual/en/language.oop5.overloading.php
UPDATE
,如果你在开行的Zend /控制器/ action.php的480
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
}
}
,并确保你的控制器扩展新生成的类
class FooController extends My_Controller_Action
{
public function indexAction()
{
// action body
}
}
,所以如果你的一些怎么叫不存在的行动__call
将运行 这种想法是不存在的有关行动只 它不会,如果工作控制器不存在
答
所以这似乎工作,但不是最好的答案,因为它只是抛弃所有参数。我可能会尝试在/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');
}
}
}
我不确定你指的是哪种'__call'方法?哪一堂课?更具体地说,这种情况下的控制器类不存在。我想引入一条新路线的原因是,我可以开始循环,并让路由器正确地捕获“:action /'后面的所有内容。然而,我意识到,这不能在bootstrap中完成,因为路由器和调度程序还没有准备好。它认为我可能需要创建一个插件或其他东西。 – 2010-04-28 19:36:37
@brett我已经更新了我的答案,希望我的回答能帮助你 – tawfekov 2010-04-29 12:24:25