4

Good morning, I have classic application, and I'll like to extend ArticleController from UserController, but when I try

class ArticleController extends UserController
{
    // ...
}

or

class ArticleController extends Application_Controllers_UserController
{

}

I got Fatal error: class ... not found... How can I extend one controller from another in Zend Framework?

tereško
  • 58,060
  • 25
  • 98
  • 150
Sebastian Busek
  • 952
  • 1
  • 14
  • 28
  • And what's the name of the controller actually? Most likely it's Application_UserController, assuming that the application is called 'Application'. – Maxim Krizhanovsky Dec 28 '11 at 18:15
  • It's worth noting that subclassing a base controller can often be avoided by using action-helpers, which tend to be more flexible. See http://stackoverflow.com/questions/5049204/is-a-good-idea-have-a-basecontroller-and-make-all-controllers-extend-that-class – David Weinraub Dec 29 '11 at 03:39

2 Answers2

4

Autoloading of controller class names is not something you get access to in your application or have much of a need for except in a case like this.

You will need to manually include/require the file that contains the controller you wish to extend.

<?php
require_once 'UserController.php'; // no adjustment to this path should be necessary

class ArticleController extends UserController
{
    // ...
}

Note that your view scripts will still be served from views/scripts/article not views/scripts/user. You can adjust the view path in each action if necessary.

As stated in the comment, you shouldn't have to change the path of the require_once statement, but you can change it as necessary (e.g. require_once APPLICATION_PATH . '/modules/test/controllers/UserController.php';)

drew010
  • 68,777
  • 11
  • 134
  • 162
1

You have to init autoloader properly before you can make such a thing for example in Bootstrap. Esspecialy when you are extending controllers in standard controllers direcotry in Zend.

$namespace = 'Application';
$basePath = APPLICATION_PATH;
$autoloader = new Zend_Loader_Autoloader_Resource(array('namespace' => $namespace, 'basePath' => $basePath));
$autoloader->addResourceTypes(array('type' => 'controllers', 'path' => '/controllers', 'namespace' => 'Controller'));

Now you can access it by Application_Controller_[YourControllerName].

If you have modular application you can allways replace 'Application' with your module name or just leave it blank.

Michael
  • 1,067
  • 8
  • 13