2013-08-01 24 views
1

我有兩個模塊(默認和移動),模塊mobile是重寫jquery mobile中的默認門戶,但控制器和操作少得多! 我想寫一個控制器插件,檢查控制器和操作是否存在於模塊移動,如果不是,我想覆蓋模塊移動默認。 我試試這個:如何檢查在控制器插件中是否存在action preDispatch

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()」

我該怎麼辦? 謝謝

回答

0

你有沒有想過如何檢查控制器/動作是否存在於任何應用程序的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; 

,並請參閱detail link

+0

不行的詳細信息!在我的情況下,控制器存在,但操作號和'$ dispatcher-> isDispatchable($ test)'總是返回'true'! – JellyBelly

+0

@JellyBelly請檢查我的更新回答 – liyakat

+1

感謝您的回覆,但是按照定義的$ className? – JellyBelly

0

我會建議你做一個靜態或動態路由,或者通過配置資源管理器,引導或通過前端控制器插件:

在bootstrap.php中定義靜態路由

實施例:

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 
} 

這有效地將用於/移動/一些控制器/一些動作路由請求/默認/一些控制器/一些動作

一些控制器一些行動應適當控制器和動作名稱所取代。

我正在使用靜態路由,如果您路由到精確的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檢查路由this link

+0

這個策略不好,因爲我必須能夠理解你要求的是什麼能夠改變要設置的佈局!:S – JellyBelly

+0

只是因爲你不明白它並不意味着它是一個不好的策略;)我會編輯我的答案,使其更加明確 –

+0

感謝您的回覆,但我不喜歡這個策略,因爲如果將來我在項目中添加新股,我不得不增加一條路線,而我希望有一個適合所有季節的方法! :D – JellyBelly