无法从zend框架2中的url获取路由名称

时间:2014-06-20 06:23:02

标签: zend-framework2 routes

我正在使用Zend Framework 2,这是我的代码:

$prevsingletrackurl = $this->getRequest()->getHeader('Referer')->getUri();

从这个在控制器方法中定义的代码中我得到了以前的URL,现在我需要知道一些这方面的信息,它包含了我自己要求的路由,控制器和方法。

2 个答案:

答案 0 :(得分:1)

您可以使用路由堆栈本身来尝试匹配它。如果请求对象匹配,它将返回RouteMatch:

use Zend\Mvc\Router\RouteMatch;

$referer = $this->getRequest()->getHeader('Referer')->getUri();
$request = clone $this->getRequest();
$request->setUri($referer);

$match = $routeStack->match($request);
if ($match instanceof RouteMatch) {
    $route = $match->getMatchedRouteName();
}

您可以从服务定位器访问路由堆栈("路由器");在根服务定位器中,它被注册为'路由器'。您可以在工厂中注入路径堆栈。例如在控制器中:

use MyModule\Controller\MyController;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class MyControllerFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $sl)
    {
        $router     = $sl->getServiceLocator()->get('Router');
        $controller = new MyController($router);

        return $controller;
    }
}

在控制器中使用它:

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Mvc\Router\Http\TreeRouteStack;

class MyController extends AbstractActionController
{
    protected $router;

    public function __construct(TreeRouteStack $router)
    {
        $this->router = $router;
    }

    protected function getRouter()
    {
        return $this->router;
    }
}

答案 1 :(得分:1)

此代码我在控制器操作方法中使用

$ request = $ this-> getRequest(); $ getHeaderReferer = $请求 - > getHeader( '的Referer');

      if (!empty($getHeaderReferer))
      {


           $prevsingletrackurl = $getHeaderReferer->getUri(); //  get previous url
             //echo "<br>previous usrl=>".$prevsingletrackurl;        
           $controller = $this->getEvent()->getRouteMatch()->getParam('controller'); // get controller name




             //**** code to get previous url route , controller name , action/method name starts


               $request->setUri($prevsingletrackurl);
               $router =$this->getServiceLocator()->get('Router');// this gives instance of /Zend/Mv/Router/Http/TreeRouteStack 

               //echo "<br>router=>".$router->toString();
               $routeMatch = $router->match($request); // this gives instance of /Zend/Mv/Router/Http/RouteMatch

               $routename="";




               if( $routeMatch instanceof RouteMatch )
               {

                     $urlwholeroutedataAr=$routeMatch->getParams();

                    if(!empty($urlwholeroutedataAr))
                    {
                         $controllerdata=$urlwholeroutedataAr['controller'];
                         $actionmethod=$urlwholeroutedataAr['action'];

                         if(array_key_exists('trackdata',$urlwholeroutedataAr))
                         {
                            $trackdata=$urlwholeroutedataAr['trackdata'];
                         }

                          $controllerdataAr=explode("\\",$controllerdata);

                          if(count($controllerdataAr)>0)
                          {
                            $controllername=end($controllerdataAr);
                          }



                    }

                     $routename=$routeMatch->getMatchedRouteName();
                     }
}