-1

I have set the variable in module.php which i can get in the layout. now i want this variable in my controller also.

 public function onBootstrap(MvcEvent $e) {
    $eventManager = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
  //  $this->initAcl($e);
     $e->attach(MvcEvent::EVENT_RENDER, array($this, 'setVariableToLayout'), 100);
}
public function setVariableToLayout(MvcEvent $event) {
    $viewModel = $event->getViewModel();
    $viewModel->setVariables(array(
        'controller' => $event->getRouteMatch()->getParam('controller'),
    ));
}

How can i get that variable in all my controller. if i try to get the variable in controller like print_r($controller) or print_r($this->controller) it gives undefined error.

This is module.php file and i have set controller as which controller is requested., function called from onBootstrap

user3157253
  • 83
  • 1
  • 6
  • where do you have that function? where do you call it from? please paste your whole file. Also, where is defined the variable that you want to send to the controller? – Carlos Robles Jan 29 '14 at 12:33
  • Erm bro, why in the world would you need a CONTROLLER variable inside a CONTROLLER? No matter which controller you're in, it's always the current controller :D – Sam Jan 29 '14 at 14:06
  • if in the place of controller, i need action variable in the controller or any other variable, then can you give me suggestion pls. – user3157253 Jan 30 '14 at 05:34

1 Answers1

0

I do not understand the need for the controller name in the view. The view should only care about the parameters it requires to render correctly; not which controller was dispatched.

I'm assuming you are doing some kind of conditional view logic if ($controller == 'foo') then this should be shouting very loudly that you need a different ViewModel for each 'state'. If they are shared view scripts then take a look into view helpers or view partials.

With the above said; The only error I can see is:

$e->attach(MvcEvent::EVENT_RENDER, ....

Should be:

$eventManager->attach(MvcEvent::EVENT_RENDER, ....

Perhaps this was a typo, however to clarify:

  • The EventManager manages references to registered event listeners.
  • The Event is a container that provides metadata regarding the event (which is then injected into the event listener when trigger() is called)
AlexP
  • 9,906
  • 1
  • 24
  • 43