Calling member function of other controller in zend framework? - php

Is it possible to call the member function of another controller in zend framework, if yes then how?
<?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.
Solution
Better idea is to define a AppController and make all usual controllers to extend AppController which further extends 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 memberFunction can be invoked from controllers extending AppController as a rule of simple inheritance.

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.

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

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);

Related

Accessing controller's function

I am using Codeigniter framework to develop a website. I am currently working on home.php view under the view folder. I need to use UserInfo() function which is inside one of the controllers. Any suggestion how to access that function?
class Welcome extends CI_Controller {
public function UserInfo(){
$this->load->model('model_user');
$data['title'] = 'Users';
$data['users'] = $this->model_user->getUser();
$this->load->view('template/users', $data);
}
}
You cant call controller method inside another controller. Its No Way to do it.
You have two way to resolve this issue
If you want to access the function which place inside the
controller, add that into an model. So by loading model you can call
it.
use redirect('welcome/UserInfo') if you just need to call the function
As You want to call controller function in other controller.In codeigniter App folder core folder exists you make a custom core controller and all other controllers extend with your custom controller
In your Custom Core controller
class CustomCore extends CI_Controller
{
/* ---YOUR FUNCTION IN CUSTOMCORE---- */
public function mycorefunc()
{
//Do something
}
}
and your all other controllers extend with custom core
class YourController extends Customcore
{
function controllerfunction()
{
$this->mycorefunc();// Call corefunction
}
}

How does a Zend controller action have access to Form subclass for instantiation?

Considering this subclass of Zend_Form
class Form_Mine extends Zend_Form
{
public function init()
{
//form
}
Then in
Class MineController extends Zend_controller_Action
{
public function formAction()
{
$form = new Form_Mine();
}
}
How does the controller know of 'Form_Mine's existence in order to be instantiated?
I understand that through the Zend_Form's constructor the function init() is called setting up the form however through what chain or routing does the controller get access to 'Form_Mine'?
The class name is significant. By default, given a class named My_Form_Mine, Zend would look for the class in file: /library/My/Form/Mine.php. My understanding is that this is handled by the autoloader: http://framework.zend.com/manual/1.12/en/zend.loader.autoloader.html

silverstripe init() function

Can someone explain this code in Silverstripe:
public function init() {
RSSFeed::linkToFeed($this->Link() . "rss");
parent::init();
}
What exactly is init function?
what parent::init();
exactly do in code
in php classes when you overwrite a method of parent class you still can call the parent class with this code, it will help you to put some code at the beginning of the real method without removing it.
you can find out more about it at php documentation
The upmost init() method is defined in the Controller class.
Then ContentController extends Controller, which overrides the Controller class's init() method, but it's also calling parent::init() on the first line. Then usually you define all your page controller classes like this (for any new page type), in the example below for the default Page_Controller class:
class Page_Controller extends ContentController {
public function init() {
parent::init();
// do your own stuff here
}
}
So this is the traditional PHP based class extension mechanism, but Silverstripe also allows you to use Extensions and Data Extensions, which is basically extending the functionality of already existing controllers, data objects. I won't go into details with this... You can find out more about this here: https://docs.silverstripe.org/en/4/developer_guides/extending/extensions/
I usually have something like this in my controller classes:
class Page_Controller extends ContentController {
public function init() {
parent::init();
// do your own stuff here
$this->extend('updateInit');
}
}
Notice the $this->extend('updateInit'); line above.
I can have another extension defined for the Page_Controller class inside a YAML config file somewhere, and than have the updateInit() method defined in that class. Example:
class Page_Controller_Extension extends Extension {
public function updateInit() {
// do some more stuff here
}
}
...and in this case you would have something like this in a YAML config file:
---
Name: siteextensions
After:
- 'framework/*'
- 'cms/*'
---
Page_Controller:
extensions:
- Page_Controller_Extension
Note that this is not really the traditional way of extending classes, like in PHP, it's more like defining some decorators for a controller class. Also, to refer to the parent, or object being decorated, you can't use just $this, you'll need to use $this->owner. Example below:
class Page_Controller_Extension extends Extension {
public function updateInit() {
// do some more stuff here
if ($this->owner->IsFeatured) {
// do something here
}
}
}
You usually decorate controllers extending the Extension class, and you extend the DataExtension class if you want to decorate DataObjects - works the same way as explained above.

Codeigniter. One __constructor to several controllers

I have some controllers, where __constructor is similar in every of it. How to avoid copy-paste of similar code and write it in a one place?
You can create a controller lets say ParentController and extends it with base controller then add __contructor in that.
Now in all of your controllers where you want this constructor just extend your controllers with the created controller ParentController.
ParentController.php:
class ParentController extends CI_Controller {
function __construct()
{
parent::__construct();
//your constructor code here..
}
}
Now the controllers in which you want the same constructor can be extended from ParentController :
ClassA.php
class ClassA extends ParentController {
function __construct()
{
parent::__construct();
}
//your first controller
}
ClassB.php
class ClassB extends ParentController{
function __construct()
{
parent::__construct();
}
//Your second controller
}
Hope this helps.
Best place for ParentController.php would be application/core/ folder.

Zend Plugin , register during predispatch but before action method is called

I am trying to figure a way to call a plugin during predispatch, but I am having trouble calling it last.
Basically I need to call this after all controller predispatch, before the action method is called.
Is this possible?
I tried calling the plugin lastly by passing a high value, but I know this is wrong. I am not looking to call this on the postDispatch.
$front->registerPlugin(new Plugin_Acl, 1000);
class Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(...)
{
// ... DO STUFF before any action method
}
}
The standard Zend_Controller_Action has an empty preDispatch() method which is called after preDispatch() on all front-controller plugins (which occurs before the controller is even instantiated) and then on all attached action-helpers, right before the action method itself. See the dispatch() methods on Zend_Controller_Front, Zend_Controller_Dispatcher_Standard, and Zend_Controller_Action for the flow.
So, if you want something to run at that point on all controllers, then you could create a base controller with a preDispatch() method containing the code you want to run.
I am doing that in dispatchLoopStartup()
class My_Plugins_Front extends Zend_Controller_Plugin_Abstract {
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request){
// Register ACL plugin if is admin controller (for eg.)
if($request->getControllerName() == 'admin'){
/**
* Set up your ACL
*/
}
// You can setting variable to see in all view files
$layout = Zend_Layout::getMvcInstance();
$view = $layout->getView();
$view->test = 'test';
}
public function dispatchLoopShutdown(){}
}
calling in predispatch seems to work for my acl:
class Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request) {
parent::preDispatch($request);
//do some stuff
}
Then it instantiated in the application.ini with the line:
resources.frontController.plugins.acl = "Controller_Plugin_Acl"
Hope this helps some...

Categories