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?

2
  • And what's the name of the controller actually? Most likely it's Application_UserController, assuming that the application is called 'Application'. Commented Dec 28, 2011 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 stackoverflow.com/questions/5049204/… Commented Dec 29, 2011 at 3:39

2 Answers 2

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';)

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.