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
>
--
View this message in context:
http://www.nabble.com/Best-Practices%3A-Eliminate-redundancy-within-various-controller-init-methods--tp21341232p21347824.html
Sent from the Zend Framework mailing list archive at Nabble.com.