2013-05-16 40 views
1

我想延長控制器,所以我的IndexController看起來像正確的方式來擴展控制器的Zend

class IndexController extends Zend_Controller_Action 
{ 
    public function IndexAction() 
    { 
     //Add a few css files 
     //Add a few js files 
    } 

    public function LoginAction() 
    { 
     //Login stuff 
    } 
} 

現在,當我嘗試這樣做:

require_once("IndexController.php"); 
class DerivedController extends IndexController 
{ 
    public function IndexAction() 
    { 
     //Override index stuff, and use the dervied/index.phtml 
    } 
} 

並調用derived/login我得到

`Fatal error: Uncaught exception 'Zend_View_Exception' \ 
with message 'script 'derived/login.phtml' not found in path` 

所以要解決這個問題,我說哦,好吧,我可以強制登錄使用自己的看法。然後我想,這是很容易的所有我的GoTa內IndexController::LoginAction做的就是添加:

$this->view->render('index/login.phtml'); 

,但它仍然試圖尋找derived/login.phtml

只是爲了擴大多一點關於這個,我只希望這是在DerivedController定義爲使用derived/<action>.phtml但一切如LoginAction使用操作<originalcontroller>/<action>.phtml

我應該做不同的事情呢?或者我錯過了一小步?

注意如果我添加derived/login.phtml或符號鏈接它從index/login.phtml它的作品。

回答

2

如果你想重新使用從IndexController所有視圖(*一個.phtml)文件,你可以覆蓋了ScriptPath的cunstructor內,它指向正確的(索引控制器)文件夾:

class DerivedController extends IndexController 
{ 

    public function __construct() 
    { 
     $this->_view = new Zend_View(); 
     $this->_view->setScriptPath($yourpath); 
    } 

[...] 

    public function IndexAction() 
    { 
     //Override inherited IndexAction from IndexController 
    } 

[...] 

} 

編輯:

嘗試使用簡單COND itional內predispatch:

class DerivedController extends IndexController 
{ 

    public function preDispatch() 
    { 
     if (!$path = $this->getScriptPath('...')) { 
      //not found ... set scriptpath to index folder 
     } 

     [...] 

    } 

[...] 

} 

這種方式,您可以檢查是否存在derived/<action>.phtml,otherwiese設置爲使用index/<action>.phtml腳本路徑。

+0

對不起,我不想重用所有'* .phtml'文件我想覆蓋它們。以及任何未被覆蓋以使用其原始控制器'.phtml'文件的動作。 –

+0

好的,你有沒有嘗試過一個簡單的條件?請參閱編輯 – simplyray

+0

但要進行編輯,我必須覆蓋每個操作。我不想覆蓋 –

2

怎麼能一個類可以擴展一個動作應該是

class DerivedController extends IndexController 

,而不是

class DerivedController extends IndexAction 
+0

對不起,這是一個類型,+1發現它 –

1

DerivedController應該擴展類IndexController不是一個函數(的indexAction)。這樣你就不需要任何require_once()

正確方法:

class DerivedController extends IndexController 
{ 
    public function IndexAction() 
    { 
     //Override inherited IndexAction from IndexController 
    } 
} 
+0

對不起,這是一個錯字 –

+0

好吧。你有沒有嘗試在DerivedController裏設置腳本路徑來指向indexcontroller的視圖文件夾? 看看'setScriptPath()' – simplyray

+0

是的,我做了,但是會做一些不同的事情,我希望登錄來獲取index/login.phtml,在派生/覆蓋派生/ 。phtml –