Hi Distrust, Why can't you simply implement it inside of showSettingsAction() method? You NEVER want to fetch data when controller instance is created immediately. In case you want to re-use some logic in other actions (not really your case as you call fetchAll() only) you have got more options: - create protected method inside of the same controller - service methods (in your case it would be SettingsTable class?) - controller helpers - ...?
To follow Mike's answer (and don't think it's correct in your use case) - do not mix service (controller) factory with other logic as he proposed. You can overwrite protected method attachDefaultListeners() in your controller https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Controller/AbstractController.php#L304 and attach listeners you need there or use shared event manager? Matus On Wed, Sep 19, 2012 at 11:39 AM, luk <l...@mierzwa.cc> wrote: > Distrust wrote > > What can I do to initialize (do some stuff) before calling the controller > > action? > > Zend have init() function, but Zend 2 doesn't. So that (for example): > > > > class BaseController extends AbstractActionController > > { > > protected $settings_table; > > protected $settings; > > > > public function __construct() > > { > > $this->settings = $this->getSettingsTable()->fetchAll(); > > } > > > > public function getSettingsTable() > > { > > if (!$this->settings_table) > > { > > $sm = $this->getServiceLocator(); > > $this->settings_table = > $sm->get('Application\Model\SettingsTable'); > > } > > > > return $this->settings_table; > > } > > } > > > > class TestController extends BaseController > > { > > // ... > > > > public function showSettingsAction() > > { > > return new ViewModel(array( > > 'settings' => $this->settings > > )); > > } > > } > > > > won't work, because the objects have not been created yet. > > To achieve that you would need to listen on the dispatch Event with high > priority that is being executed prior to any Contoller logic. Please try > this code in your Module.php. > > namespace ModuleName; > > use ModuleNama\Controller\MyController; > use Zend\EventManager\EventManagerInterface; > > class Module > { > > public function getControllerConfig() > { > return array( > 'factories' => array( > 'ModuleName\Controller\MyController' => > function($sm) { > $events = > $sm->getServiceLocator()->get('EventManager'); > $controller = new MyController(); > $events->attach('dispatch', > function ($e) use ($controller) { > $request = > $e->getRequest(); > $method = > $request->getMethod(); > > // your own initialization > logic here > if > ($controller->params()->fromRoute('paramName', false)) { > return; > } > }, 100); // run before controller > action logic > > > $controller->setEventManager($events); > return $controller; > } > ), > ); > } > } > > > > > ----- > Cheers, > -- > Luke Mierzwa > -- > View this message in context: > http://zend-framework-community.634137.n4.nabble.com/ZF2-Doing-something-before-calling-the-controller-action-tp4656922p4656923.html > Sent from the Zend Framework mailing list archive at Nabble.com. > > -- > List: fw-general@lists.zend.com > Info: http://framework.zend.com/archives > Unsubscribe: fw-general-unsubscr...@lists.zend.com > > >