Zend Framework - Extending Controllers

I want my controller i.e. Page_IndexController, extended base controller.

For example:

class Page_IndexController extends Content_IndexController {
}

      

However, the autoloader doesn't seem to understand that this is a controller class at any point - I get the error Fatal error: Class 'Content_IndexController' not found

The first question is, how do I fix this?

I can fix this temporarily by require_once'ing a generic content controller, but this is hardly ideal.

The next problem is if my page controller has a custom script view for the action, it doesn't work.

But if I extend the controller and I call for example "listAction" on the page controller, but that action is implemented in Content_IndexController

, it still looks for the script list view in the "scripts" page controllers.

Second question: how do I configure my controller to use its parent view script if it doesn't have its own?

+2


source to share


4 answers


If your application can find the Page_IndexController, you probably have a Page module. If you are not using modules in your application, you should name your controllers PageController and ContentController, not Page_IndexController, ... So the solution is to register the Content_ namespace with an autoloader.



As for the second question. You can extend the provided ViewRenderer controller helper and override the script's view search methods so that they can search elsewhere if needed. You just need to transfer your caretaker to the front controller. To pass your own ViewRenderer to a controller, see Advanced Usage Examples .

+3


source


The autoloader can't find your controller because you haven't told it where to look. Content_IndexController

is not in your "library" folder (I assume inside a content module)

I would suggest to create a class My_Controller_IndexBase

in your library that inherits both Content_IndexController

and Page_IndexController

.

Did a bit more research on the topic of script submission. You can change the script's view paths at runtime init()

somewhere. I'm sure this will probably need to be done in the ViewRenderer, but may also work inside the controller initialization / action code.



$this->view->setScriptPath(
  array(
   realpath(APPLICATION_PATH+'/../path/to/other/module/views'),
  ) + $this->view->getScriptPath());

      

Script paths are processed Last In First Out according to Zend_View_Abstract

+2


source


For the second question:

If you don't want to write your own ViewRenderer, you can use $ this-> renderScript ('parent / index.phtml') to render a specific kind of script. You could call this in your child controllers, instead of letting the views render for you automatically, or if your child controllers rely on the parent controller to do the rendering script, you can simply host this in your parent controllers.

0


source


I am doing this mode.

Frist I will register a new plugin name in my index.php in the public folder:

/public/index.php

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('modelo_');

      

Secound I create a new folder to host my controller

/library/modelo/

      

Third, I create my controller model and put it in the created folder and rename it.

class Modelo_ModeloController extends Zend_Controller_Action
{

protected  $_db = null;
protected $_menu = null;
protected $_util = null;
protected $_path = null;

... actions to my model

public function inicial(){}

}

      

and i extend this class im my application

class Sispromo_PedidoController extends Modelo_ModeloController
{
public function init(){}
....
}

      

0


source







All Articles