Skip to content

Instantly share code, notes, and snippets.

@jonathanmaron
Created May 26, 2012 04:07
Show Gist options
  • Save jonathanmaron/2792133 to your computer and use it in GitHub Desktop.
Save jonathanmaron/2792133 to your computer and use it in GitHub Desktop.
Custom \Zend\Mvc\Controller\ActionController that passes commonly used variables to the View
<?php
namespace MyNamespace\Mvc\Controller;
use Zend\Mvc\MvcEvent,
Zend\View\Model\ViewModel,
Zend\Date\Date,
MyNamespace\Exception\InvalidArgumentException;
class ActionController extends \Zend\Mvc\Controller\ActionController
{
protected $locale;
protected $session;
protected $view;
protected function attachDefaultListeners()
{
parent::attachDefaultListeners();
$this->events()->attach('dispatch', array($this, 'preDispatch') , 100);
$this->events()->attach('dispatch', array($this, 'postDispatch'), -100);
}
public function preDispatch(MvcEvent $event)
{
$routeMatch = $event->getRouteMatch();
$locale = $routeMatch->getParam('locale');
if (0 === strlen($locale)) {
throw new InvalidArgumentException('No locale has been specified.');
}
$this->locale = $locale;
$this->session = null;
$this->setViewModelVariablesInternationalization($event)
->setViewModelVariablesSession($event)
->setViewModelVariablesMvc($event)
->setViewModelVariablesUri($event);
}
public function postDispatch(MvcEvent $event)
{
}
/**
* Pass language and locale related variables to ViewModel
*
* @param MvcEvent $event
* @return \MyNamespace\Mvc\Controller\ActionController
*/
protected function setViewModelVariablesInternationalization(MvcEvent $event)
{
if (null === $this->view) {
$this->view = new ViewModel();
}
$this->view->setVariable('locale', $this->locale);
$this->view->setVariable('date', new Date(null, null, $this->locale));
$dateFormat = new \StdClass();
$dateFormat->time = Date::TIME_LONG; // MyNamespace uses only these
$dateFormat->date = Date::DATE_LONG; // time, date and datetime
$dateFormat->datetime = Date::DATETIME_LONG; // formats
$this->view->setVariable('dateFormat', $dateFormat);
return $this;
}
/**
* Pass session related variables to ViewModel
*
* @param MvcEvent $event
* @return \MyNamespace\Mvc\Controller\ActionController
*/
protected function setViewModelVariablesSession(MvcEvent $event)
{
if (null === $this->view) {
$this->view = new ViewModel();
}
$this->view->setVariable('session', $this->session);
return $this;
}
/**
* Pass MVC related variables to ViewModel
*
* @param MvcEvent $event
* @return \MyNamespace\Mvc\Controller\ActionController
*/
protected function setViewModelVariablesMvc(MvcEvent $event)
{
if (null === $this->view) {
$this->view = new ViewModel();
}
$this->view->setVariable('module', $event->getRouteMatch()->getParam('module'));
$this->view->setVariable('controller', $event->getRouteMatch()->getParam('controller'));
$this->view->setVariable('action', $event->getRouteMatch()->getParam('action'));
return $this;
}
/**
* Pass Request Uri related variables to ViewModel
*
* @param MvcEvent $event
* @return \MyNamespace\Mvc\Controller\ActionController
*/
protected function setViewModelVariablesUri(MvcEvent $event)
{
if (null === $this->view) {
$this->view = new ViewModel();
}
$uri = new \StdClass();
$uri->absolute = $this->getRequest()->getUri();
$uri->relative = $this->getRequest()->getRequestUri();
$uri->scheme = parse_url($uri->absolute, PHP_URL_SCHEME);
$uri->host = parse_url($uri->absolute, PHP_URL_HOST);
$uri->port = parse_url($uri->absolute, PHP_URL_PORT);
$uri->user = parse_url($uri->absolute, PHP_URL_USER);
$uri->password = parse_url($uri->absolute, PHP_URL_PASS);
$uri->path = parse_url($uri->absolute, PHP_URL_PATH);
$uri->query = parse_url($uri->absolute, PHP_URL_QUERY);
$this->view->setVariable('uri', $uri);
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment