Reputation: 1032
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?
Upvotes: 4
Views: 3757
Reputation: 1217
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.
Upvotes: 1
Reputation: 69957
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';
)
Upvotes: 4