2017-08-31 60 views
0

我在Drupal 8网站上工作。在一个页面中,我有一个包含GET方法的表单的PHP块。PHP恢复完整的URL drupal 8

在接收信息的页面中,我有一个看起来像site/page?param1=value1&param2=value2的网址。我需要得到value2

我试过很多解决方案,但我没有找到解决办法:

$_GET['id']; 
// returns : null 

\Drupal::service('path.current')->getPath(); 
// returns : '/node/4' 

$_SERVER['PHP_SELF']; 
// returns : 'site/page' 

$_SERVER['REQUEST_URI']; 
// returns : 'site/page' 

$_SERVER['argv']; 
// returns : null 

$_SERVER['HTTP_REFERER']; 
// returns : 'http://site/node/4/edit' 

\Drupal::request()->query->all(); 
// returns : array (size=0) empty 

$path = \Drupal::request()->getpathInfo(); 
// returns : '/page' 

\Drupal::request()->getRequestUri(); 
// returns : 'site/page' 

\Drupal::request()->attributes->get('_system_path'); 
// returns : null 

\Drupal::service('path.alias_manager')->getAliasByPath($current_path); 
// returns : '/page' 

$_SERVER['QUERY_STRING']; 
// returns : '' 

\Drupal::destination()->getAsArray(); 
// returns : 'site/page' 

有人可以帮我找到这个值2这是在网址是什么?

回答

1

可以使用多种服务检索URL参数(S)如:

  • request_stack
  • path.current
  • current_route_match

在这里,我告诉你如何从让你value2param2使用request_stack

// Initialize the service. 
$requestStack = \Drupal::service('request_stack'); 

// Get your Master Request. 
$masterRequest = $requestStack->getMasterRequest(); 

// Get the value from a GET parameter. 
$value = $masterRequest->query->get('param2'); 

再进一步:


希望它会帮助你的!

+0

当我尝试这个时,我得到了一个“null”。我认为问题出在这个形式上,我会试着去看这个。谢谢。 – Elehyan

+0

什么是真正的URL?你能否在这里添加你的表单代码?并为您的表单提供动力的路由文件(routing.yml)。 –

+0

你也可以尝试使用'CurrentRequest'而不是'MasterRequest'。例如。 '$ request = $ requestStack-> getCurrentRequest();'如果有效,你在一个SubRequest中,检查我给你的关于请求的链接。 –