3
votes

in my application i have a route (Literal or segment) for every action. i am not using one global route for everything so as a result the number of routes has grown hugely with 44+ modules (and more in future) .

It is my understanding (from what i have seen in the code) that in every page request zend goes throw all this routes in a array ans searches for a match witch could be bottleneck for the application (am i right?)

So i was thinking why not cache the matched routes in a db table with index to speed up the search ?

FIRST QUESTION : would this make the systems performance better?

so my first problem is skipping the system route matching mechanism. this is what i tried but it did not work :

public function onBootstrap(MvcEvent $e)
    {
        $em = StaticEventManager::getInstance();
        $em->attach('Zend\Mvc\Application', MvcEvent::EVENT_ROUTE, array($this, 'onRoute'), 100);        
    } 

public function onRoute(MvcEvent $e)
{
    //var_dump($e->getRouteMatch());//->null routing has not been done yet
    /* @var $router \Zend\Mvc\Router\Http\TreeRouteStack */
    $router = $e->getRouter();
    //-------------------------------------created a dummy route
    $routeMatch = new \Zend\Mvc\Router\RouteMatch(array(
        'controller' => 'Links\Controller\Items',
        'action' => 'view',
        'catId' => 0
    ));
    $routeMatch->setMatchedRouteName('app/links');
    $e->setRouteMatch($routeMatch);//set the dummy route
    //--------------------------------------------PROBLEM HERE
    //detach the onRoute event from routeListener
    $e->getApplication()
        ->getServiceManager()
        ->get('RouteListener')
        ->detach($e->getApplication()->getEventManager());
}

the detach method is executed but the onRoute event still gets executed and matches the url to the correct route. so how to bypass(skip|detach) route matching ?

1

1 Answers

3
votes

The reason you can't detach the listener is because you're already in the route event. By that point all the listeners have been marshalled together and queued for execution.

Why not instead take the listener out of the equation beforehand?

public function onBootstrap(MvcEvent $e)
{
    $app = $e->getApplication();
    $events = $app->getEventManager();
    $shared = $events->getSharedManager();
    $services = $app->getServiceManager();

    $routeListener = $services->get('RouteListener');
    $routeListener->detach($events);                

    $shared->attach('Zend\Mvc\Application', MvcEvent::EVENT_ROUTE, array($this, 'onRoute'), 100);
}