Extend the controller from the controller in another module?

I have this module called "olo" that processes all our online orders.

Now I have created a new module called "olosec" because I want to make another version with a small modified stream and some other changes in some controllers.

Is it possible to extend the controller in "olosec" by the controller in "olo"?

At the moment I tried

class Olosec_CartController extends Olo_CartController 

What causes the error, for example

 Warning: include_once(Olo/CartController.php): failed to open stream: No such file or directory in /httpdocs/library/Zend/Loader.php on line 146 Warning: include_once(): Failed opening 'Olo/CartController.php' for inclusion. bla bla bla (include path) bla bla bla 

My directory structure is something like this (thanks tree \F \A and EditPlus ++ )

 +---application | +---views | | +---scripts | | +---layouts | | | +---default | | | +---admin | | +---languages | | +---helpers | +---modules | | +---admin | | +---mobile | | +---olo | | | +---controllers | | | IndexController.php | | | MenuController.php | | | CartController.php | | | OrderlistController.php | | | | | | | +---models | | | \---views | | | +---helpers | | | \---scripts | | | +---index | | | +---menu | | | +---cart | | | \---orderlist | | \---olosec | | +---controllers | | | IndexController.php | | | MenuController.php | | | CartController.php | | | OrderlistController.php | | | | | +---models | | \---views | | +---helpers | | \---scripts | | +---index | | +---menu | | +---cart | | \---orderlist | +---models | +---controllers | \---configs +---library +---public | +---cli | \---default +---tests \---data 

Update

I used this "nasty" hack that works

 require_once( APPLICATION_PATH . '/modules/olo/controllers/CartController.php'); 

Update @Rakesh

I have this in my bootstrap.

 function _initAutoloader() { $autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader->setFallbackAutoloader(true); return $autoloader; } 

In my application.ini application

 autoloadernamespaces.0 = "Zend" autoloadernamespaces.1 = "My" autoloadernamespaces.2 = "Something" 
+6
source share
2 answers

Why not create a custom library folder for common classes

 application/ library/ < for common classes 

If you use some classes not only in one controller, but also in many places of your project, this is a good approach.

You just need to add this new application/library/ folder to your include path in the boostrap file.

Another approach is to have an action helper. But, as I described, a shared class folder should be a good solution. However, I found some interesting resources, most of them relate to cross-modular coding, but they can help you anyway http://zend-framework-community.634137.n4.nabble.com/Code-re-use-across- modules-td668554.html and How to use the same models in different modules in the Zend Framework?


Let me describe a different approach.

 class BasicController extends Zend_Controller_Action { public static $variable = ''; public function init() { self::$variable = 'assign value'; } } class HomeController extends BasicController { public function indexAction() { $bioVar = parrent::$variable; } } 

This is better than just extending controllers, as they represent actions, and each action has a corresponding script view. However, all of your classes must be registered with the autoloader.

+1
source

If you have a class instance from Zend Autoloader, the error should go away.

You should have the following code in the bootstrap.php file:

 protected function _initAutoloader() { $autoloader = new Zend_Application_Module_Autoloader(array( 'namespace' => '', 'basePath' => APPLICATION_PATH . '/modules', )); return $autoloader; } 

In general, controllers are loaded through Zend_Controller_Dispatcher, you should use Zend_Application_Module_Autoloader to create an instance of the class from other controllers.

+1
source

Source: https://habr.com/ru/post/887343/


All Articles