dqvzfp6468 2013-12-02 06:43
浏览 61

在ZF2路由未找到异常时调度Controller的操作

I want want dispatch another controller action when Zf2 raise route not found exception to display my custom page ( i dont want to display a custom error page). I am working on dynamic url from database in Zf2 which will occur only when route not found. i added a event in bootstrap function

$event->attach('route', array($this, 'loadConfiguration'), 2);

in loadConfiguration function i added to load

public function loadConfiguration(MvcEvent $e){
    $application   = $e->getApplication();
    $sm = $application->getServiceManager();
    $router = $sm->get('router');
    $request = $sm->get('request');
    $matchedRoute = $router->match($request);
if (null == $matchedRoute) { 
        $request_uri = $e->getRequest()->getrequestUri();
        $dbAdaptor = $e->getApplication()->getServiceManager()->get('Zend\Db\Adapter\Adapter');
        $url_table = new UrlMappingTable($dbAdaptor);
        $url_data = $url_table->find($request_uri);
        $controller = $url_data['controller'];
        $action = $url_data['action'];
        $id = $url_data['post_id'];
        $original_url = $url_data['original_url'];
        $alias = $sm->get('Application\Router\Alias');

            $alias->setNavigation($original_url);

        if(isset($url_data)){
        $url = $e->getRouter ()->assemble (array('controller' => $controller,
                                                     'action' => $action ,
                                                      'id' => $id,
                                              ), array (
                                            'name' => 'myurl'
                                            ) );
       }

 print 'no route match';
 }
}

after getting the controller and action i just want the dispatcher to forward this controller.

  • 写回答

2条回答 默认 最新

  • dongseshu0698 2013-12-02 08:28
    关注

    Just something not really answering your question:

    I am working on dynamic url from database in Zf2 which will occur only when route not found.

    There are two ways you can achieve this much more efficiently. If there aren't much routes, you can load them before the route event. For example, you query on bootstrap all your database routes and inject them into the route stack.

    Another way is creating a "catch all" route which always will match after all routes failed. Then you don't have a "route not found" but your default route is matched. This will then look for a matching database record. If none found, you just return a 404 response.

    In case #1, the controller mapped by your database route is directly dispatched. In the second case, you are in your "database controller" and want to dispatch your controller mapped by the database route, you use the forward plugin:

    public function matchAction()
    {
        // Fetch route from db here
    
        if (!$match) {
            $this->getResponse()->setStatusCode(404);
            return;
        }
    
        return $this->forward($match->getController, array(
            'action' => $match->getAction()
        ));
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)