forge($presenter, $method = 'view', $auto_filter = null, $view = null)
The forge method returns a new Presenter object.
Static | Yes | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Parameters |
|
|||||||||||||||
Returns | a new Presenter object | |||||||||||||||
Example |
|
The Presenter class acts as an object wrapper for "views", and is used to abstract all logic related to the view away from the controller. Read more about using presenters. Like a Controller, a Presenter supports before() and after() methods, which you can use for code generic to all methods for view prepping.
The forge method returns a new Presenter object.
Static | Yes | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Parameters |
|
|||||||||||||||
Returns | a new Presenter object | |||||||||||||||
Example |
|
The get_view method returns the View instance associated with the Presenter object.
Static | No |
---|---|
Parameters | None |
Returns | The associated View object |
Example |
|
The view method is the default method which is called when the Presenter is rendered. It contains the logic to prep the view for rendering.
Static | No |
---|---|
Parameters | None |
Example | See the Presenter overview page. |
A presenter can contain multiple prepping methods, which are used when you need multiple sets of logic for generating the view. You could for example have a custom method that generates the view without headers and footers, or one that creates a custom view optimized for mobile devices. It allows you to keep the controller generic, it doesn't need to know what output has to be generated by the presenter.
The set_view method can be used to change the View associated with the presenter.
Static | No | ||||||
---|---|---|---|---|---|---|---|
Parameters |
|
||||||
Example |
|
When you use this method, any variables set on or bound to the original View object will be transferred to the new View.
The Presenter class is interchangeable with the View class in your code. This means that if you start with Views, and later realize you need to have additional view prepping logic and you want to use a Presenter instead, you don't have to change your controller code, other than forging a Presenter instead of a View.
To support this, the Presenter exposes the set(), set_safe(), bind(), auto_filter() and render() methods of the associated View object. Is also has magic getter and setters to access and set properties on the associated View object.
The Presenter doesn't support the static methods set_global() and bind_global(), if you need global variables for your views, you still have to set them on the View class. For the Presenter, this is transparent.
If you want to extend the Presenter to be able to swap View instances after the Presenter object has been created, know that the presenter doesn't have its own data container. Instead, it uses the associated View object to store all data, which means that if you swap that View object by a new one, you lose all variables set on it!