Router Match not returning matchedRouteName

牧云@^-^@ 提交于 2019-12-24 12:46:23

问题


I have been slowly working my way through a few issues with the help of this website. I think I am onto my last problem now. In my boostrap I have the following code:

$eventManager = $event->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
    $serviceManager = $event->getApplication()->getServiceManager();

    $eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, function($event) use ($serviceManager ){

        $exception = $event->getParam('exception');
        if ($exception) {
            do {
                $serviceManager->get('Logger')->crit(
                    sprintf(
                        "%s:%d %s (%d) [%s]\n",
                        $exception->getFile(),
                        $exception->getLine(),
                        $exception->getMessage(),
                        $exception->getCode(),
                        get_class($exception)
                    )
                );
            } while ($ex = $exception->getPrevious());

            $response = $event->getResponse();
            $response->setHeaders(
                $response->getHeaders()->addHeaderLine('Location', "/")
            );
            $response->setStatusCode(302);
            $response->sendHeaders();
            return $response;
        } else {
            //no route - redirect to cms handler
            $newEvent = clone $event;
            $eventManager = $newEvent->getApplication()->getEventManager();
            $routeMatch = new Router\RouteMatch(array('controller'=>'Website\Controller\Index','action'=>'index'));

            $event->stopPropagation(TRUE);
            $newEvent->setRouteMatch($routeMatch);
            $newEvent->getResponse()->setStatusCode(200);

            $eventManager->trigger('dispatch', $newEvent);

        }
    });

This all works fine except that $routeMatch is not returning matchedRouteName - and so I get a 404. I know that the contoller and action names are correct as for now I am just redirecting to homepage and by echoing out $routeMatch on "Displatch" the same info is coming back. So, it seems simply using the trigger "dispatch" is not enough. Any help appreciated. Thanks Adam

Further Info: I have confirmed that the dispatch is actually working - however, the triggered 404 is still being acted upon. That is a 404 header status is being set and the default 404 handler (view etc) is being displayed.

Solved by adding: $newEvent->getResponse()->setStatusCode(200);

来源:https://stackoverflow.com/questions/32430595/router-match-not-returning-matchedroutename

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!