Quantcast

$e->getRouteMatch() is NULL !!

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

$e->getRouteMatch() is NULL !!

sina miandashti-3
hi

im wondering

why this returned null  in my onBootstrap method in module.php

$e->getRouteMatch();


i want to get requested controller and action in module.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: $e->getRouteMatch() is NULL !!

Marco Pivetta
You need to do that after routing. You can attach an event listener to your
application, listening to `\Zend\Mvc\MvcEvent::EVENT_ROUTE` with low
priority (so it happens after routing). Then you should be able to access a
RouteMatch instance. It may be null anyway if nothing has been matched.

To do what I described above, I think you can do something like following:

public function onBoostrap($e)
{
    $e->getEventManager()->attach(
        \Zend\Mvc\MvcEvent::EVENT_ROUTE,
        function($e) {
            /* $e is the MvcEvent here, so do whatever you want here,
$e->getRouteMatch() will work if a route was matched */
        },
        -1000
    );
}
Marco Pivetta

http://twitter.com/Ocramius

http://marco-pivetta.com



On 6 August 2012 10:08, sina miandashti <[hidden email]> wrote:

> hi
>
> im wondering
>
> why this returned null  in my onBootstrap method in module.php
>
> $e->getRouteMatch();
>
>
> i want to get requested controller and action in module.php
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: $e->getRouteMatch() is NULL !!

Jurian Sluiman-3
In reply to this post by sina miandashti-3
2012/8/6 sina miandashti <[hidden email]>

> hi
>
> im wondering
>
> why this returned null  in my onBootstrap method in module.php
>
> $e->getRouteMatch();
>
>
> i want to get requested controller and action in module.php
>

Hi Sine,

The onBootstrap listener listens to the "bootstrap" event. That is a part
of the process to initiate all modules of your application, independent of
what (kind of) request you are sending. The routing is another stage and
happens after bootstrapping.

If you need to fetch the controller and action, you create a listener in
your application for the route event. This listener will be attached during
bootstrap:

use Zend\EventManager\EventInterface;
use Zend\Mvc\MvcEvent;

class Module
{
    public function onBootstrap(EventInterface $e)
    {
        $app = $e->getApplication();
        $em  = $app->getEventManager();

        $em->attach(MvcEvent::EVENT_ROUTE, function($e) {
            $routeMatch = $e->getRouteMatch();

            $controller = $routeMatch->getParam('controller');
            $action     = $routeMatch->getParam('action');
        });
    }
}

This will give you the controller (as a string value, not the class itself)
and the action.

--
Jurian Sluiman
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: $e->getRouteMatch() is NULL !!

Marco Pivetta
Whooops! Take Jurian's solution, mine is flawed :)

Marco Pivetta

http://twitter.com/Ocramius

http://marco-pivetta.com



On 6 August 2012 10:23, Jurian Sluiman <[hidden email]> wrote:

> 2012/8/6 sina miandashti <[hidden email]>
>
> > hi
> >
> > im wondering
> >
> > why this returned null  in my onBootstrap method in module.php
> >
> > $e->getRouteMatch();
> >
> >
> > i want to get requested controller and action in module.php
> >
>
> Hi Sine,
>
> The onBootstrap listener listens to the "bootstrap" event. That is a part
> of the process to initiate all modules of your application, independent of
> what (kind of) request you are sending. The routing is another stage and
> happens after bootstrapping.
>
> If you need to fetch the controller and action, you create a listener in
> your application for the route event. This listener will be attached during
> bootstrap:
>
> use Zend\EventManager\EventInterface;
> use Zend\Mvc\MvcEvent;
>
> class Module
> {
>     public function onBootstrap(EventInterface $e)
>     {
>         $app = $e->getApplication();
>         $em  = $app->getEventManager();
>
>         $em->attach(MvcEvent::EVENT_ROUTE, function($e) {
>             $routeMatch = $e->getRouteMatch();
>
>             $controller = $routeMatch->getParam('controller');
>             $action     = $routeMatch->getParam('action');
>         });
>     }
> }
>
> This will give you the controller (as a string value, not the class itself)
> and the action.
>
> --
> Jurian Sluiman
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: $e->getRouteMatch() is NULL !!

sina miandashti
thanks guys

but how can i access that $action outside that function($e)

i want to assign it to viewmodel


--
________________
Sincerely
Sina Miandashti
www.InTheMix.ir  Admin
www.teamatec.com Head PHP Developer
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: $e->getRouteMatch() is NULL !!

sina miandashti-3
In reply to this post by Marco Pivetta
thanks guys

but how can i access that $action outside that function($e)

i want to assign it to viewmodel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: $e->getRouteMatch() is NULL !!

Marco Pivetta
In reply to this post by sina miandashti
Well, that is simply a closure, a callback.
You could even use array($this, 'someCallbackMethod') instead... But you
cannot really access the route match before that event, so the operation
has to happen within a callback, be it a closure or any valid PHP callback.

Marco Pivetta

http://twitter.com/Ocramius

http://marco-pivetta.com



On 6 August 2012 10:37, sina miandashti <[hidden email]> wrote:

> thanks guys
>
> but how can i access that $action outside that function($e)
>
> i want to assign it to viewmodel
>
>
> --
> ________________
> Sincerely
> Sina Miandashti
> www.InTheMix.ir  Admin
> www.teamatec.com Head PHP Developer
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: $e->getRouteMatch() is NULL !!

sina miandashti-3
ok

fixed by this :

$em->attach(MvcEvent::EVENT_ROUTE, function($e) {
            $routeMatch = $e->getRouteMatch();
            $viewModel = $e->getViewModel();
            $viewModel->action = $routeMatch->getParam('action');


        });

thankkss

On Mon, Aug 6, 2012 at 1:09 PM, Marco Pivetta <[hidden email]> wrote:

> Well, that is simply a closure, a callback.
> You could even use array($this, 'someCallbackMethod') instead... But you
> cannot really access the route match before that event, so the operation
> has to happen within a callback, be it a closure or any valid PHP callback.
>
>
> Marco Pivetta
>
> http://twitter.com/Ocramius
>
> http://marco-pivetta.com
>
>
>
> On 6 August 2012 10:37, sina miandashti <[hidden email]> wrote:
>
>> thanks guys
>>
>> but how can i access that $action outside that function($e)
>>
>> i want to assign it to viewmodel
>>
>>
>> --
>> ________________
>> Sincerely
>> Sina Miandashti
>> www.InTheMix.ir  Admin
>> www.teamatec.com Head PHP Developer
>>
>
>
Loading...