ZF2 - 获取Controller中的当前URL

Sco*_*ott 4 php zend-framework2

这似乎应该是一个简单的任务.我需要Controller中一个函数的当前URL.可以从多个动作调用此函数,最终目标是设置表单的动作属性.(旁注:如果URL以'#'开头,IE似乎不会发送ajax请求).

我觉得我的google-fu今天已经关闭,因为我找不到一个很好的方法来做这个Zend Framework 2.我现在有这条线,但感觉非常笨重:

 $this->url()->fromRoute(
    $this->getServiceLocator()
    ->get('Application')
    ->getMvcEvent()
    ->getRouteMatch()
    ->getMatchedRouteName()
  );
Run Code Online (Sandbox Code Playgroud)

Jef*_*fwa 6

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

$这个 - > Request()方法 - > getUriString()

提供控制器扩展Zend\Mvc\Controller\AbstractActionController.

注意:这将输出整个URL,如下所示:

 http://example.com/en/path/subpath/finalpath?test=example
Run Code Online (Sandbox Code Playgroud)


小智 5

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

http://example.com/en/path/subpath/finalpath?test=example
Run Code Online (Sandbox Code Playgroud)

而你只想要这个:

/en/path/subpath/finalpath?test=example
Run Code Online (Sandbox Code Playgroud)

您可以简单地执行以下操作:$this->getRequest()->getRequestUri()

指定我的 Request 对象是一个实例

\ZF\ContentNegotiation\Request