Extending a controller from an existing controller class in the Zend Framework

Good morning, I have a classic application and I want to extend ArticleController from UserController, but when I try

class ArticleController extends UserController { // ... } 

or

 class ArticleController extends Application_Controllers_UserController { } 

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

+4
source share
2 answers

Autoload of controller class names is not something that you access in your application or have a great need, except in cases like this.

You will need to manually include / request the file containing the controller that you want to extend.

 <?php require_once 'UserController.php'; // no adjustment to this path should be necessary class ArticleController extends UserController { // ... } 

Please note that your view scripts will still be served from views / scripts / articles, not views / scripts / users. You can customize the viewing path in each action, if necessary.

As stated in the comment, you do not need to change the path of the require_once statement, but you can change it if necessary (for example, require_once APPLICATION_PATH . '/modules/test/controllers/UserController.php'; )

+4
source

You must start the autoloader correctly before you can do such a thing, for example, in Bootstrap. Esspecialy when you extend controllers in the standard controller manager 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 using Application_Controller_ [YourControllerName].

If you have a modular application, you can always replace the "Application" with your module name or just leave it empty.

+1
source

Source: https://habr.com/ru/post/1388343/


All Articles