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