Re: [fw-general] Re: ZF2 Doing something before calling the controller action
-- Distrust trashbin.woj...@gmail.com wrote (on Wednesday, 19 September 2012, 04:04 AM -0700): Matus Zeman wrote Why can't you simply implement it inside of showSettingsAction() method? Because I need a universal method to initialize various elements (before action), without duplicating my code in every other controller action. Settings were just an example. I've covered a variety of options here a few weeks ago: http://mwop.net/blog/2012-07-30-the-new-init.html -- Matthew Weier O'Phinney Project Lead| matt...@zend.com Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc -- List: fw-general@lists.zend.com Info: http://framework.zend.com/archives Unsubscribe: fw-general-unsubscr...@lists.zend.com
[fw-general] Re: ZF2 Doing something before calling the controller action
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
Re: [fw-general] Re: ZF2 Doing something before calling the controller action
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
[fw-general] Re: ZF2 Doing something before calling the controller action
Matus Zeman wrote Why can't you simply implement it inside of showSettingsAction() method? Because I need a universal method to initialize various elements (before action), without duplicating my code in every other controller action. Settings were just an example. Matus Zeman wrote - create protected method inside of the same controller In some cases this would be enough, but not in every. Matus Zeman wrote You can overwrite protected method attachDefaultListeners() in your controller https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Controller/AbstractController.php#L304 Works like a charm, thanks! -- View this message in context: http://zend-framework-community.634137.n4.nabble.com/ZF2-Doing-something-before-calling-the-controller-action-tp4656922p4656931.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
[fw-general] Re: ZF2 Doing something before calling the controller action
Distrust wrote Matus Zeman wrote Why can't you simply implement it inside of showSettingsAction() method? Because I need a universal method to initialize various elements (before action), without duplicating my code in every other controller action. Settings were just an example. For universal solution you may want to try this one, it will work for all Controllers within single Module: class Module { // Evan's changing layout for specific module only public function init(ModuleManager $moduleManager) { $sharedEvents = $moduleManager-getEventManager()-getSharedManager(); $sharedEvents-attach(__NAMESPACE__, 'dispatch', function($e) { $controller = $e-getTarget(); // do your stuff with controller }, 100); } } - Cheers, -- Luke Mierzwa -- View this message in context: http://zend-framework-community.634137.n4.nabble.com/ZF2-Doing-something-before-calling-the-controller-action-tp4656922p4656934.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