Getting uri using EventManager

zend-eventmanager
zend-mvc

#1

hi,I’ve been trying to figure out how zend-eventManager would work throughout the zend-mvc. I try to get some parameters from my uri and output them as some info in my view. I have found that I should utilize eventManager component to add my params for more than once. I am up to use MvcEvent::EVENT_RENDER as the event and I want to write an Event Listener to react against triggering the event.
my Listener is code below:

namespace Blog\Events;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\Mvc\MvcEvent;

class Example implements ListenerAggregateInterface
{
protected $listener;
public function attach(EventManagerInterface $events,$priority = 1){
    $sharedManager = $events->getSharedManager();
    $this->listener[] = $sharedManager->attach(__NAMESPACE__
        ,MvcEvent::EVENT_RENDER
        ,[$this,'addParams']
        ,1000);
}

public function addParams(MvcEvent $event){
    // Get request URI
    $uri = $event->getRequest()->getUri();
    echo $uri;
}
}

I already know I must register this Event Listener to module entry at Module.php in my Module, but I do NOT understand how it works with all provided explanation.
I acted like this in Module.php:

    public function onBootstrap(MvcEvent $event)
{
    $eventManager = $event->getApplication()->getEventManager();
    $sharedEventManager = $eventManager->getSharedManager();
}

I’ve seen passing ModuleManager instances and EventInterface and MvcEvent to the method but I have no ideat what the differences are!!!
Anyway,how should I keep going to work it out?!?!?!?!?
How should I register my Listener correctly?


Nested breadcrumb using zend-navigation
#2

Sorry, I don’t understand concrete question?

How should I register my Listener correctly?

You can check my code and how I registered an listener


#3

I just want to get the results provided with my action in controller that is sent to view script, this results are supposed to be my requested URL and in the view script I want to get some strings in my URL and use them in navigation breadcrumb or somewhere else. I understood that the RENDER event does so. but I don’t know how to register it correctly.


#4

This wrong:

use instead: (This example includes is the entire module class.)

namespace Example;

use Example\Listener\ExampleListener;
use Zend\EventManager\EventInterface;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\Mvc\MvcEvent;

class Module implements ConfigProviderInterface, BootstrapListenerInterface
{
    /**
     * @inheritdoc
     */
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    /**
     * {@inheritdoc}
     *
     * @param EventInterface|MvcEvent $e
     */
    public function onBootstrap(EventInterface $e)
    {
       $application = $e->getApplication();

       $aggregate = new ExampleListener();
       $aggregate->attach($application->getEventManager());
    }
}

#5

But you can get requested URL in the view file without any hard work?


#6

I see another problem: bind some output on the URL is a bad idea. If the URL changes, the view-script must also be changed.

Btw. you can use the URL view helper:

<?= $this->url() ?>