Ish*_*Ish 24 php zend-framework
是否可以在zend框架中调用另一个控制器的成员函数,如果是,那么如何?
<?php
class FirstController extends Zend_Controller_Action {
public function indexAction() {
// general action
}
public function memberFunction() {
// a resuable function
}
}
Run Code Online (Sandbox Code Playgroud)
这是另一个控制器
<?php
class SecondController extends Zend_Controller_Action {
public indexAction() {
// here i need to call memberFunction() of FirstController
}
}
Run Code Online (Sandbox Code Playgroud)
请解释我如何从第二个控制器访问memberFunction().
更好的想法是定义AppController并使所有常用控制器扩展AppController,进一步扩展Zend_Controller_Action.
class AppController extends Zend_Controller_Action {
public function memberFunction() {
// a resuable function
}
}
class FirstController extends AppController {
public function indexAction() {
// call function from any child class
$this->memberFunction();
}
}
Run Code Online (Sandbox Code Playgroud)
现在memberFunction可以从AppController作为简单继承规则扩展的控制器调用.
Fer*_*yer 25
控制器不是以这种方式使用的.如果要在当前控制器之后执行其他控制器的操作,请使用以下方法:_forward()
// Invokes SecondController::otherActionAction() after the current action has been finished.
$this->_forward('other-action', 'second');
Run Code Online (Sandbox Code Playgroud)
请注意,这仅适用于操作方法("memberAction"),而不适用于任意成员函数!
如果SecondController::memberFunction()跨多个控制器需要执行某些操作,请将该代码放在操作助手或库类中,以便两个控制器都可以访问共享功能而无需相互依赖.