This allows you to easily differentiate between page not found and application errors in your error handler. You should override this functionality if you wish to perform other operations. For instance, if you wish to display an error message, you might write something like this:. Another possibility is that you may want to forward on to a default controller page:. As an example, if you are storing your view object in a registry, you may want to modify your initView method with code resembling the following:.
Hopefully, from the information in this chapter, you can see the flexibility of this particular component and how you can shape it to your application's or site's needs. The Dispatcher. Action Helpers.
Made with by awesome contributors. This website is built using zend-expressive and it runs on PHP 7. We use cookies to allow you to dismiss dialogs such as the Laminas Project notification. Toggle navigation. Action Controllers. Object Initialization. Except there's one specific and often-needed use case where this fails: if you want to wire listeners to the controller's event manager.
Why does this fail with the event manager? Because when we're in the constructor, we don't yet have an EventManager instance! The event manager instance is injected after instantiation. As such, we need to attach to it once we know we have an event manager.
Which is… when it's set. This can be done very simply by overriding the setEventManager method. In the next example, we'll define a listener for the "dispatch" event that redirects if certain criteria is not met. If you remember from the Website Operation chapter, the application has several life stages represented by events. You may write an event listener function or class and register it in your module entry point.
When an event is triggered, your listener method or class will be called allowing you to do something useful. There are two ways to register an event listener within the Module class: either with the help of Module 's init method or with the help of its onBootstrap method. The difference between init method and onBootstrap method is that the init method is called earlier than onBootstrap , before all other modules are initialized; while onBootstrap is called once all modules are initialized.
In the following examples, we use init method. To show you how to subscribe to an event, let's create an event listener that will react on Dispatch event and set a different layout template for all controllers of the module:.
The init function always occurs before the onBootstrap function. Since the purpose of init is to initialize the module eg. However, onBootstrap is run after all modules have been initialized, and it can listen for different events. I use onBootstrap to check my access control list which users have access to what resources and restrict access accordingly, like this:. Posts saved by boris. How I can get access to my module config from the controller?
0コメント