php 在zend框架中调用其他控制器的成员函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/886291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:13:12  来源:igfitidea点击:

Calling member function of other controller in zend framework?

phpzend-framework

提问by Ish

Is it possible to call the member function of another controller in zend framework, if yes then how?

是否可以在zend框架中调用另一个控制器的成员函数,如果是,那么如何?

<?php
class FirstController extends Zend_Controller_Action {
    public function indexAction() {
         // general action 
    }   

    public function memberFunction() {
         // a resuable function
    }
}

Here's another controller

这是另一个控制器

<?php
class SecondController extends Zend_Controller_Action {
    public indexAction() {
         // here i need to call memberFunction() of FirstController
    }
}

Please explain how i can access memberFunction() from second controller.

请解释我如何从第二个控制器访问 memberFunction()。

Solution

解决方案

Better idea is to define a AppController and make all usual controllers to extend AppController which further extends Zend_Controller_Action.

更好的想法是定义一个 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();
    } 
}

Now memberFunctioncan be invoked from controllers extending AppControlleras a rule of simple inheritance.

现在memberFunction可以从AppController作为简单继承规则扩展的控制器调用。

回答by Ferdinand Beyer

Controllers aren't designed to be used in that way. If you want to execute an actionof the other controller afteryour current controller, use the _forward()method:

控制器并非旨在以这种方式使用。如果要当前控制器之后执行另一个控制器的操作,请使用以下方法:_forward()

// Invokes SecondController::otherActionAction() after the current action has been finished.
$this->_forward('other-action', 'second');

Note that this only works for action methods(“memberAction”), not arbitrary member functions!

请注意,这仅适用于操作方法(“memberAction”),而不适用于任意成员函数!

If SecondController::memberFunction()does something that is needed across multiple controllers, put that code in a action helper or library class, so that both controllers can access the shared functionality without having to depend on each other.

如果SecondController::memberFunction()需要跨多个控制器执行某些操作,请将该代码放在操作助手或库类中,以便两个控制器可以访问共享功能而不必相互依赖。

回答by Rob Allen

You should consider factoring out the code into either an action helper or to your model so that it can be called from both controllers that need it.

您应该考虑将代码分解为动作助手或模型,以便可以从需要它的两个控制器中调用它。

Regards,

问候,

Rob...

抢...

回答by Mike Walkowiak

I would suggest you to follow DRY and move those functions to common library place. For example create in library folder

我建议您遵循 DRY 并将这些函数移动到公共库位置。例如在库文件夹中创建

My/Util/ 

and file

和文件

CommonFunctions.php

then call your class

然后打电话给你的班级

My_Util_CommonFunctions

and define your methods

并定义你的方法

Now you can call them from any place in the code using your new namespace which you have to register.

现在,您可以使用必须注册的新命名空间从代码中的任何位置调用它们。

$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace(array('My_'));

in any controller you can call your custom methods by using:

在任何控制器中,您都可以使用以下方法调用自定义方法:

My_Util_CustomFunctions::yourCustomMethod($params);