0

下面是我的Bootstrap类中定义的一个函数。在 Zend 进行路由和调度的方式中,我一定遗漏了一些基本的东西。我想要完成的事情很简单:对于任何/foo/bar/*因任何原因无法分派的请求 try /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();
    }
4

2 回答 2

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');
    }
  }
}
于 2010-04-28T20:21:23.333 回答
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 
        }
}

并确保您的控制器扩展您新创建的类

class FooController extends My_Controller_Action
{
   public function indexAction()
    {
        // action body
    }
}

因此,如果某些您称之为不存在的动作的方法__call将运行这个想法是关于不存在的动作,只有在控制器不存在时它才会起作用

于 2010-04-27T19:00:34.313 回答