views:

585

answers:

3

I'd like to have my controller - i.e. Page_IndexController - extend a base controller.

For example;

class Page_IndexController extends Content_IndexController {
}

However, it seems the autoloader doesn't pick up the fact it's a controller class at any point - I get the error Fatal error: Class 'Content_IndexController' not found

First question: How do I fix this?

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

The next issue is that if my Page controller has it's own view script for an action, it works no problem.

But if I'm extending a controller, and I call for example 'listAction' on the Page controller, but this action is implemented in Content_IndexController, it still looks for the list view script in the page controllers "scripts" directory.

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

+1  A: 

The auto loader can't find your controller because you haven't told it where to search. The Content_IndexController isn't in your "library" folder (I assume its inside of the Content module)

What I would suggest is creating a My_Controller_IndexBase class in your library folder that both Content_IndexController and Page_IndexController inherit.

Did a little more research on the topic of the view script. You could change up the view's script paths during init() somewhere. I'm pretty sure this would probably need to be done in a ViewRenderer - but might also work inside the controller's init/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 the Zend_View_Abstract

gnarf
+2  A: 

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

As for the second question. You can extend the provided ViewRenderer controller action helper and override methods for finding the view script so they can look in other places if needed. You just have to pass your viewrenderer to the front controller. For passing your own ViewRenderer to the controller take a look at Advanced Usage Examples.

Goran Jurić
Registering the namespace only helps if your modules are in /library/Content/IndexController.php - it still doesn't seem to pick it up in /application/modules/content/controllers/IndexController.php ... am I missing something here?
searbe
A: 

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 view script. You could call that in your child controllers instead of letting the views be rendered for you automatically, or if your child controllers rely on the parent controller to do the rendering of the script you can just place that in your parent controllers.

smack0007