# Getting uri using EventManager

**URL:** <https://discourse.laminas.dev/t/getting-uri-using-eventmanager/385>\
**Category:** Components & MVC\
**Tags:** zend-mvc, zend-eventmanager\
**Created:** [December 1, 2017, 11:32am UTC](https://discourse.laminas.dev/t/getting-uri-using-eventmanager/385 "2017-12-01T11:32:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![erfan](https://avatars.discourse-cdn.com/v4/letter/e/c4cdca/32.png) [@erfan](https://discourse.laminas.dev/u/erfan)\
**Post date:** [December 1, 2017, 11:32am UTC](https://discourse.laminas.dev/t/getting-uri-using-eventmanager/385/1 "2017-12-01T11:32:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tasmaniski](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tasmaniski/32/70_2.png) [@tasmaniski](https://discourse.laminas.dev/u/tasmaniski)\
**Post date:** [December 4, 2017, 8:51am UTC](https://discourse.laminas.dev/t/getting-uri-using-eventmanager/385/2 "2017-12-04T08:51:33Z")

</div>

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

> **[GitHub - tasmaniski/zend-layout-change: Set different layout per module -...](https://github.com/tasmaniski/zend-layout-change)**
>
> Set different layout per module - Zend Framework 3 - GitHub - tasmaniski/zend-layout-change: Set different layout per module - Zend Framework 3

---

<div class="post-metadata">

**Author:** ![erfan](https://avatars.discourse-cdn.com/v4/letter/e/c4cdca/32.png) [@erfan](https://discourse.laminas.dev/u/erfan)\
**Post date:** [December 6, 2017, 9:56am UTC](https://discourse.laminas.dev/t/getting-uri-using-eventmanager/385/3 "2017-12-06T09:56:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [December 6, 2017, 11:12am UTC](https://discourse.laminas.dev/t/getting-uri-using-eventmanager/385/4 "2017-12-06T11:12:12Z")

</div>

This wrong:

> [@erfan](#):
>
> ```
> public function onBootstrap(MvcEvent $event)
> {
> $eventManager = $event-&gt;getApplication()-&gt;getEventManager();
> $sharedEventManager = $eventManager-&gt;getSharedManager();
> }
> 
> ```

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());
    }
}

```

---

<div class="post-metadata">

**Author:** ![tasmaniski](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tasmaniski/32/70_2.png) [@tasmaniski](https://discourse.laminas.dev/u/tasmaniski)\
**Post date:** [December 6, 2017, 11:54am UTC](https://discourse.laminas.dev/t/getting-uri-using-eventmanager/385/5 "2017-12-06T11:54:19Z")

</div>

> [@erfan](#):
>
> requested URL

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

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [December 6, 2017, 1:55pm UTC](https://discourse.laminas.dev/t/getting-uri-using-eventmanager/385/6 "2017-12-06T13:55:19Z")

</div>

> [@tasmaniski](#):
>
> But you can get requested URL in the view file without any hard work?

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() ?>

```
