ZF2 - 在控制器中获取当前 URL

ZF2 - Get current URL in Controller

这看起来应该是一个简单的任务。我需要来自控制器内函数的当前 URL。可以从多个操作调用此函数,最终目标是设置表单的操作属性。 (旁注:如果 URL 以“#”开头,IE 似乎不会发送 ajax 请求)。

我觉得我的 google-fu 今天没电了,因为我找不到做这个 Zend Framework 2 的好方法。我目前有这条线,但感觉很笨重:

 $this->url()->fromRoute(
    $this->getServiceLocator()
    ->get('Application')
    ->getMvcEvent()
    ->getRouteMatch()
    ->getMatchedRouteName()
  );

你不能直接从请求对象中获取 URI:

$this->getRequest()->getUriString()

如果您的控制器扩展 Zend\Mvc\Controller\AbstractActionController

注意:这将输出整个 URL,像这样:

 http://example.com/en/path/subpath/finalpath?test=example

如果你的请求路由是这样的:

http://example.com/en/path/subpath/finalpath?test=example

而你只想要这个:

/en/path/subpath/finalpath?test=example

你可以简单地做:$this->getRequest()->getRequestUri()

To specify my Request object is an instance of

\ZF\ContentNegotiation\Request