I have this module called 'olo' which handles all our online ordering stuff.
Now I have made a new module called 'olosec' because I wish make a different version with a slight changed flow, and some other changes in some of the controllers.
Is it possible for me to extend a controller in 'olosec' with a controller in 'olo'?
As of now I have tried
class Olosec_CartController extends Olo_CartController
Which throws an error like
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 have used this "nasty" hack which 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
autoloadernamespaces.0 = "Zend"
autoloadernamespaces.1 = "My"
autoloadernamespaces.2 = "Something"
Why not have a custom library folder for common classes
application/
library/ < for common classes
If you are using some classes not only in one controller but at many places in your project this is a good approach.
You just have to add this new application/library/
folder to your include path
in your boostrap file.
Another approach is to have an action helper. But as I described the common classes folder should be a good solution. However I found some interesting resources, most of them are about cross module coding but they might help you anyway http://zend-framework-community.634137.n4.nabble.com/Code-re-use-across-modules-td668554.html and How to Use Same Models in Different Modules in Zend Framework?
Let me describe another 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 simply extending controllers because they represent actions and each action has a corresponding view script. However all your classes should be registered in autoloader.
If you have instantiate the class from Zend Autoloader, the error should go away.
You should have the following code in your bootstrap.php file:
protected function _initAutoloader()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH . '/modules',
));
return $autoloader;
}
In general, the controllers are loaded via Zend_Controller_Dispatcher, you should use Zend_Application_Module_Autoloader to instantiate the class from other controllers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With