Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending the IndexController with a BaseController in Zend

I'm trying to extend my controllers with a global base controller as such:

class BaseController extends Zend_Controller_Action {
 // common controller actions
    public function listAction() {
        // do stuff
    }
}

class IndexController extends BaseController {
 // index controller specific actions
}

class LoginController extends BaseController {
 // login controller specific actions
}

But I get this error: PHP Fatal error: Class 'BaseController' not found in /var/www/Zend/project/application/controllers/IndexController.php on line 3

Any ideas on how to get Zend to "see" this controller?

like image 965
BillA Avatar asked Oct 02 '09 16:10

BillA


2 Answers

the quick solution that does not take advantage of the autoloader functionality is to require_once '/path/to/BaseController.php' in the index-controller file.

If you have set-up autocontroller, then it can not find it, so you should consider checking what's wrong. Try the previous approach and inform on results.

like image 167
Dimitrios Mistriotis Avatar answered Sep 21 '22 20:09

Dimitrios Mistriotis


Autoloader

Setup the autoloader and register your library which should be besides the Zend library with the autoloader like so (in your bootstrap.php after setting the include path):

//AutoLoad loads classes automatically if they are used
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('Mylibrary_');

Zend naming conventions

Then you should rename your BaseController as follows

/Zend (folder)
/Mylibrary (folder)
    /Controller (folder)
        Action.php <-- this is your basecontroller file

which contains:

class Mylibrary_Controller_Action extends Zend_Controller_Action
{
}

and your normal controllers in the controller folder:

class IndexController extends Mylibrary_Controller_Action
{
}

so basically when you want to extend the framework you keep a parallel structure in your own library.

like image 28
markus Avatar answered Sep 20 '22 20:09

markus