I am trying to figure out how to use the event manager for this task.
Basically, all of my controllers extend from AppController. AppController has a method, isPjax
that returns true or false based on if the request was made by my jQuery Pjax plugin. If the request is pjax, then the controller will by default set $render_for_pjax
to true. Each individual controller is able to set this to false if it wishes.
Using the beforeRender
event of the view, I would like to grab the current controller, check if $render_for_pjax
is true or false, and call the prepareForPjax
method if it is true.
In the code below, how can I access the controller instance within this event callback?
Thank you.
// Set up the view component.
$di->set('view', function() {
$view = new \Phalcon\Mvc\View();
$events_manager= new \Phalcon\Events\Manager();
// Attach event listener to view.
$events_manager->attach('view', function($event, $view) {
if($event->getType() == 'beforeRender') {
// I want to use the current view's controller right here.
}
});
$view->setViewsDir('../app/views/');
$view->setEventsManager($events_manager);
// Registering Volt as template engine
$view->registerEngines([
".phtml" => 'Phalcon\Mvc\View\Engine\Php'
]);
return $view;
}, true);