2013-05-16 42 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 –