-- Bart McLeod <[email protected]> wrote
(on Thursday, 08 January 2009, 10:26 AM +0100):
> You can of course have a base custom controller, but you do not need to.
> 
> Depending on what you need exactly you can use either an action helper in the
> init method or a plugin or both a plugin and an action helper.

Also, you can create an action helper that has an init() or
preDispatch() method, and register it in your bootstrap. This is a nice
way to build code that can be re-used across controllers without needing
a custom base controller (as action helpers have introspection to the
current controller).


> public function init(){
>     $this->_helper->myInit(); //instead of your six lines of code
> }
> 
> Bart
> 
> Giuliano Riccio schreef:
> 
>     You can make a custom controller to make all the others... something like
>     this:
> 
>         class My_Controller_Action extends Zend_Controller_Action
>         {
>             public function init()
>             {
>                 // your common code
>             }
>         }
> 
>     Then you will use it like this:
> 
>         class IndexController extends My_Controller_Action
>         {
>             // optional function to use to add more stuff to the default init
>             public function init()
>             {
>                 // some code
>                 parent::init(); // use this anywhere inside this function to
>     call the default init
>                 // some code
>             }
>         }
> 
>     Hope it helps ;)
> 
>     Giuliano
> 
>     jasonzfw wrote:
> 
> 
>         Hi,
> 
>         Been using the ZFW for a few weeks now, and love it. I have a pretty 
> good
>         grasp on the key features, however there's one obstacle I can't seem 
> to
>         surpass. My site is broken into about 7 different controllers, and I 
> store
>         all configuration data in config.ini.
> 
>         Currently within each controller init() method I redundantly include 
> the
>         same 5-6 lines of code which perform various calculations, such as
>         determining the number of registered users. This information is
>         subsequently displayed within the page header. For instance, you'll 
> find
>         this line within each of my controllers' init() methods:
> 
>         $this->view->totalUsers = $user->getUserCount();
> 
>         (obviously this data is cached using Zend_Cache, I'm just keeping it
>         simple here)
> 
>         For that matter, several of my controller actions use config.ini
>         parameters, so the following line is currently found in every 
> controllers'
>         init() method:
> 
>         $this->config = Zend_Registry::get('config');
> 
>         I've seemingly tried everything, including creating a custom front
>         controller plugin and defining a preDispatch() method. However the
>         variable scope seems limited to that method, as I'm unable to retrieve
>         $this->config within my controllers.
> 
>         Surely there's a recommended way to manage these lines of code in a 
> single
>         location? While this works, I'd much rather avoid having to 
> redundantly
>         maintain the same lines of code in each controller action.
> 
>         Thank you!
>         Jason
> 
> 
> 
> 
> 

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

Reply via email to