-- Jaka Jančar <[email protected]> wrote
(on Tuesday, 03 February 2009, 09:27 PM +0100):
> I have the following files:
>
> application/controllers/CrudController.php (abstract class with methods 
> list, show, edit...)
> application/views/scripts/crud/list.phtml (templates for CrudController)
> application/views/scripts/crud/show.phtml
> ...
>
> application/controllers/FooController.php (extends CrudController)
> application/modules/a/controllers/FooController.php (extends  
> CrudController)
> application/modules/b/controllers/BarController.php (extends  
> CrudController)
>
> I want all of these non-abstract controllers to use templates from the  
> abstract one, since I don't want to have 10 copies of the templates for 
> generic item listing, etc.
>
> How can I achieve this?
>
> By default, ViewRenderer will try to render, for example
>   application/modules/b/views/scripts/foo/list.html
> instead of the "generic"
>   application/views/scripts/crud/list.html
>
> I have found 2 approaches so far, but both have problems:
>
> A)
> In init() of the abstract CrudController:
>     $this->_helper->viewRenderer->setViewBasePathSpec(Bootstrap:: 
> $root.'/application/views');
>     $this->_helper->viewRenderer- 
> >setViewScriptPathSpec('crud/:action.:suffix');
>     $this->_helper->viewRenderer->initView();
>
> This will work, but the problem is that when an action is redirected,  
> the ViewRenderer is all messed up. For example, if an error ocurrs, it  
> will try to render crud/error.phtml instead of error/error.phtml.
>
> I could do pre/postDispatch magic here, bit it seems dirty.

Actually, that's a good way to do it -- have the controller clean up
after itself. Ideally, we should likely have the ErrorController setup
some sane defaults -- and you could, potentially, have it do just that.

The above is exactly the solution I would have suggested, btw. Your (B)
is problematic on many levels.

> B)
> Also in init of the abstract CrudController:
>     $request = $this->getRequest();
>     $request->setModuleName('default');
>     $request->setControllerName('crud');
>     $this->_helper->viewRenderer->initView();
>
> This will make the view render properly, but will mess up a whole bunch 
> of other things, such as URLs generated with the url helper that don't 
> reset the params.
>
>
> Any suggestions on how I can achieve this behavior (one set of view  
> scripts for many controllers)?

-- 
Matthew Weier O'Phinney
Software Architect       | [email protected]
Zend Framework           | http://framework.zend.com/

Reply via email to