Custom logic in every controller and action in ZF2, how?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Custom logic in every controller and action in ZF2, how?

sinimix
This post has NOT been accepted by the mailing list yet.
I'm currently testing multilingual feature in ZF2 and configured router to be like: /en/controller/action, /de/controller/action etc.

The problem is that in every action and controllers I need to use completely the same code for getting language part of the URL.

Where (and how) can I put the code to be performed for each action be default?
Reply | Threaded
Open this post in threaded view
|

Re: Custom logic in every controller and action in ZF2, how?

samsonasik
This post has NOT been accepted by the mailing list yet.
You can attach 'Zend\Mvc\Controller\AbstractActionController'

namespace YourModule;

use Zend\Mvc\MvcEvent;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $sharedEvents        = $e->getApplication()->getEventManager()->getSharedManager();
        $sharedEvents->attach('Zend\Mvc\Controller\AbstractActionController','dispatch',
             function($e) {
                   $routematch = $e->getRouteMatch();
//                   echo $match->getParam('language');

                  $controller = $e->getTarget();
                  $controller->lang = $match->getParam('language');
        }, 1000);
    }
}

and all in your controller, you can call like this :
class YourController extends AbstractActionController
{
     public function indexAction()
{
    echo $this->lang;
}
}