Inheritance: extends Zend\EventManager\AbstractListenerAggregate
 /**
  * {@inheritDoc}
  *
  * Create and return an InjectTemplateListener instance.
  *
  * @return InjectTemplateListener
  */
 public function __invoke(ContainerInterface $container, $name, array $options = null)
 {
     $listener = new InjectTemplateListener();
     $config = $container->get('config');
     if (isset($config['view_manager']['controller_map']) && is_array($config['view_manager']['controller_map'])) {
         $listener->setControllerMap($config['view_manager']['controller_map']);
     }
     return $listener;
 }
 /**
  * {@inheritDoc}
  *
  * Create and return an InjectTemplateListener instance.
  *
  * @return InjectTemplateListener
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $listener = new InjectTemplateListener();
     $config = $serviceLocator->get('Config');
     if (isset($config['view_manager']['controller_map']) && is_array($config['view_manager']['controller_map'])) {
         $listener->setControllerMap($config['view_manager']['controller_map']);
     }
     return $listener;
 }
Example #3
0
 /**
  * @param $namespace
  * @return string
  */
 protected function deriveControllerSubNamespace($namespace)
 {
     $pos = strpos($namespace, '\\Controller\\');
     if (false === $pos) {
         return parent::deriveControllerSubNamespace($namespace);
     }
     return str_replace('\\', '/', substr($namespace, $pos + 12));
 }
 /**
  * Inject a template into the view model, if none present
  *
  * Template is derived from the controller found in the route match, and,
  * optionally, the action, if present.
  *
  * @param  MvcEvent $e
  * @return void
  */
 public function injectTemplate(MvcEvent $e)
 {
     $routeMatch = $e->getRouteMatch();
     $controller = $e->getTarget();
     if (is_object($controller)) {
         $controller = get_class($controller);
     }
     if (!$controller) {
         $controller = $routeMatch->getParam('controller', '');
     }
     if (strpos($controller, 'Ctrl\\Module') !== 0) {
         return;
     }
     parent::injectTemplate($e);
 }
 /**
  * Inject a template into the view model, if none present
  *
  * Template is derived from the controller found in the route match, and,
  * optionally, the action, if present.
  *
  * @param  MvcEvent $e
  * @return void
  */
 public function injectTemplate(MvcEvent $e)
 {
     /**
      * This condition fix the template name derived from the controller
      * We have to use the service name insteadof the controller class because
      * proxy manager generate a random name
      */
     $controller = $e->getTarget();
     $routeMatch = $e->getRouteMatch();
     if ($controller instanceof ProxyInterface) {
         $e->setTarget($routeMatch->getParam('controller', ''));
     }
     parent::injectTemplate($e);
     if ($controller instanceof ProxyInterface) {
         $e->setTarget($controller);
     }
 }
 /**
  * Inject a template into the view model, if none present
  *
  * Template is derived from the controller found in the route match, and,
  * optionally, the action, if present.
  *
  * @param  MvcEvent $e
  * @return void
  */
 public function injectTemplate(MvcEvent $e)
 {
     $e->getRouteMatch()->setParam('action', null);
     parent::injectTemplate($e);
 }
Example #7
0
 public function getInjectTemplateListener()
 {
     $listener = new InjectTemplateListener();
     if (isset($this->config['controller_map'])) {
         $listener->setControllerMap($this->config['controller_map']);
     }
     return $listener;
 }
 /**
  * {@inheritdoc}
  */
 public function injectTemplate(MvcEvent $e)
 {
     parent::injectTemplate($e);
 }
Example #9
0
 /**
  * Run an action from the specified controller and render it's associated template or view model
  * @param string $expr
  * @param array $attributes
  * @param array $options
  * @return string
  */
 public function __invoke($expr, $attributes, $options)
 {
     $serviceManager = $this->serviceLocator;
     $application = $serviceManager->get('Application');
     //parse the name of the controller, action and template directory that should be used
     $params = explode(':', $expr);
     $controllerName = $params[0];
     $actionName = 'not-found';
     if (isset($params[1])) {
         $actionName = $params[1];
     }
     //instantiate the controller based on the given name
     $controller = $serviceManager->get('ControllerLoader')->get($controllerName);
     //clone the MvcEvent and route and update them with the provided parameters
     $event = $application->getMvcEvent();
     $routeMatch = clone $event->getRouteMatch();
     $event = clone $event;
     $event->setTarget($controller);
     $routeMatch->setParam('action', $actionName);
     foreach ($attributes as $key => $value) {
         $routeMatch->setParam($key, $value);
     }
     $event->setRouteMatch($routeMatch);
     $actionName = $routeMatch->getParam('action');
     //inject the new event into the controller
     if ($controller instanceof InjectApplicationEventInterface) {
         $controller->setEvent($event);
     }
     //test if the action exists in the controller and change it to not-found if missing
     $method = AbstractActionController::getMethodFromAction($actionName);
     if (!method_exists($controller, $method)) {
         $method = 'notFoundAction';
         $actionName = 'not-found';
     }
     //call the method on the controller
     $response = $controller->{$method}();
     //if the result is an instance of the Response class return it
     if ($response instanceof Response) {
         return $response->getBody();
     }
     //if the response is an instance of ViewModel then render that one
     if ($response instanceof ModelInterface) {
         $viewModel = $response;
     } elseif ($response === null || is_array($response) || $response instanceof \ArrayAccess || $response instanceof \Traversable) {
         $viewModel = new ViewModel($response);
     } else {
         return '';
     }
     //inject the view model into the MVC event
     $event->setResult($viewModel);
     //inject template name based on the matched route
     $injectTemplateListener = new InjectTemplateListener();
     $injectTemplateListener->injectTemplate($event);
     $viewModel->terminate();
     $viewModel->setOption('has_parent', true);
     //render the view model
     $view = $serviceManager->get('Zend\\View\\View');
     $output = $view->render($viewModel);
     return $output;
 }