我正在尝试扩展一个控制器,所以我的IndexController看起来像
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`
为了解决这个问题,我说,哦,没问题,我可以强制login使用它自己的视图。我想这很简单在IndexController::LoginAction
中我只需要添加:
$this->view->render('index/login.phtml');
但它仍然试图寻找derived/login.phtml
。
为了进一步扩展这一点,我只希望在DerivedController
中定义的Actions使用derived/<action>.phtml
,而其他所有内容(如LoginAction
)使用<originalcontroller>/<action>.phtml
我应该做不同的事情吗?还是我错过了一小步?
注意如果我从index/login.phtml
添加derived/login.phtml
或符号链接,它可以工作。
如果您想重用IndexController
中的所有视图(*.phtml)文件,您可以覆盖构造函数中的ScriptPath并将其指向正确的(indexcontroller)文件夹:
class DerivedController extends IndexController
{
public function __construct()
{
$this->_view = new Zend_View();
$this->_view->setScriptPath($yourpath);
}
[...]
public function IndexAction()
{
//Override inherited IndexAction from IndexController
}
[...]
}
编辑:
尝试在predispatch中使用一个简单的条件:
class DerivedController extends IndexController
{
public function preDispatch()
{
if (!$path = $this->getScriptPath('...')) {
//not found ... set scriptpath to index folder
}
[...]
}
[...]
}
这样可以检查derived/<action>.phtml
是否存在,否则将脚本路径设置为使用index/<action>.phtml
。
一个类如何扩展一个Action呢?
class DerivedController extends IndexController
和
class DerivedController extends IndexAction
DerivedController
应该扩展class IndexController
而不是一个函数(IndexAction)。这样你就不需要任何require_once()
了。
正确的方法:
class DerivedController extends IndexController
{
public function IndexAction()
{
//Override inherited IndexAction from IndexController
}
}