2010-04-27 102 views
0

下面是我的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(); 
    } 

回答

0

如果我理解你的想法正確 會嘗試使用__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將運行 這種想法是不存在的有關行動只 它不會,如果工作控制器不存在

+0

我不確定你指的是哪種'__call'方法?哪一堂課?更具體地說,這種情況下的控制器類不存在。我想引入一條新路線的原因是,我可以開始循環,並讓路由器正確地捕獲「:action /'後面的所有內容。然而,我意識到,這不能在bootstrap中完成,因爲路由器和調度程序還沒有準備好。它認爲我可能需要創建一個插件或其他東西。 – 2010-04-28 19:36:37

+0

@brett我已經更新了我的答案,希望我的回答能幫助你 – tawfekov 2010-04-29 12:24:25

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'); 
    } 
    } 
} 
相關問題