Calling member function of other controller in zend framework?

前端 未结 3 1052
挽巷
挽巷 2020-12-08 07:32

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



        
相关标签:
3条回答
  • 2020-12-08 08:04

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

    // 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!

    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.

    0 讨论(0)
  • 2020-12-08 08:15

    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...

    0 讨论(0)
  • 2020-12-08 08:16

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

    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);
    
    0 讨论(0)
提交回复
热议问题