Click here to load reader
Upload
bo-andersen
View
169
Download
2
Embed Size (px)
Citation preview
ZF2 Controller Pluginsby Bo Andersen
What are controller plugins?The same as view helpers, except controller plugins are for controllers
Controller plugins are particularly useful for operations that need to be performed repeatedly
E.g. redirecting, accessing logged in user’s information, forwarding requests, etc.
ZF2 ships with a number of useful controller plugins for common use cases
Accessible within views like this: $this->somePlugin(‘parameter’)
As the above example suggests, it is possible to write your own controller plugins
Example: Zend\Mvc\Controller\Plugin\Identitypublic function testAction()
{
if ($user = $this->identity()) {
// The user is logged in. Access user info in $user
} else {
// User is not logged in
}
}
Example: Zend\Mvc\Controller\Plugin\Layoutpublic function testAction() {
$this->layout()->setTemplate('layout/admin');
// ...
}
Example: Zend\Mvc\Controller\Plugin\UrlLike with the view helper, this controller plugin generates URLs for configured routes
public function testAction() {
$url = $this->url()->fromRoute('route-name', $parameters);
// ...
}
Example: Zend\Mvc\Controller\Plugin\Redirectpublic function testAction() {
return $this->redirect()->toRoute('front-page');
}
public function testAction() {
return $this->redirect()->toUrl('http://codingexplained.com');
}
public function testAction() {
return $this->redirect()->refresh(); // Refreshes to current route
}
Example: Zend\Mvc\Controller\Plugin\Forwardpublic function testAction() {
$json = $this->forward()->dispatch('ArticleController', array(
'action' => 'getArticle',
'articleId' => $articleId, // Pass custom parameters
));
return array(
'article' => json_decode($json),
);
}
Example: Zend\Mvc\Controller\Plugin\Paramspublic function testAction() {
$articleId = $this->params()->fromRoute('articleId');
$source = $this->params()->fromQuery('source');
$username = $this->params()->fromPost('username');
$userAgent = $this->params()->fromHeader('User-Agent');
// or
$articleId = $this->params('articleId');
}
Example: Zend\Mvc\Controller\Plugin\FlashMessengerpublic function loginAction() {
$this->flashMessenger()->addMessage('You are now logged in.');
return $this->redirect()->toRoute('logged-in');
}
public function loginSuccessAction() {
$return = array('success' => true);
$flashMessenger = $this->flashMessenger();
if ($flashMessenger->hasMessages()) {
$return['messages'] = $flashMessenger->getMessages();
}
return $return;
}
Other controller pluginsZend\Mvc\Controller\Plugin\AcceptableViewModelSelector
Can be used to select an appropriate view model based on user defined criteria which will be tested against the Accept header in the request
For example, if the Accept header includes application/json, then the controller action can return JSON - or in the case of application/xml, it can return XML
Zend\Mvc\Controller\Plugin\PostRedirectGet
When a user submits a form with the POST method, browsers will warn the user about re-submitting the form if the user navigates back
The PostRedirectGet plugin can be used to prevent this
It works by storing the form data in the session and redirects the user to perform a GET request
The form data can then be retrieved from the session and the processing can be done
Creating your own controller pluginAs with view helpers, one can create one’s own controller plugin. However, this is outside the scope of this presentation, as it is rarely needed, and especially not at this stage.
Actually, creating a custom controller plugin is very easy. If you are curious to learn more, then you can find a simple example at the link below. http://tinyurl.com/zf2-custom-controller-plugin
that’s allThis presentation is part of my Zend Framework 2 online course.
Get 50% discount by navigating to the below URL!
https://www.udemy.com/zend-framework-2-from-beginner-to-professional/?couponCode=SS50