-
-
Save csymlstd/c322f79a90b623e3dff9681d899ce2bd to your computer and use it in GitHub Desktop.
Drupal 8: how to create a sub-request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\content_helper\Controller\SubRequestController; | |
$httpKernel = \Drupal::service('http_kernel.basic'); | |
$requestStack = \Drupal::requestStack(); | |
$sub_request = new SubRequestController($httpKernel, $requestStack); | |
$api_response = $sub_request->subRequest( | |
'/jsonapi/field_config/field_config', | |
'GET', | |
[ | |
'id' => [ | |
'condition' => [ | |
'path' => 'id', | |
'operator' => 'IN', | |
'value' => [ | |
'5d44a3e4-7229-4dc9-8912-8c3857d7108f', | |
'48536695-34bc-4a1a-a3b1-dcfaedccdff0' | |
] | |
] | |
] | |
] | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\content_helper\Controller; | |
use Drupal\Core\Controller\ControllerBase; | |
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; | |
use Symfony\Component\HttpKernel\HttpKernelInterface; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\RequestStack; | |
/** | |
* Class SubRequest. | |
* | |
* @package Drupal\content_helper\Controller | |
*/ | |
class SubRequestController extends ControllerBase implements ContainerInjectionInterface { | |
/** | |
* Symfony\Component\HttpKernel\HttpKernelInterface definition. | |
* | |
* @var Symfony\Component\HttpKernel\HttpKernelInterface | |
*/ | |
protected $httpKernel; | |
/** | |
* @var \Symfony\Component\HttpFoundation\RequestStack | |
*/ | |
protected $requestStack; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function __construct(HttpKernelInterface $http_kernel, RequestStack $request_stack) { | |
$this->httpKernel = $http_kernel; | |
$this->requestStack = $request_stack; | |
} | |
/** | |
* Performs a subrequest. | |
* | |
* @param string $path | |
* Path to use for subrequest. | |
* @param string $method | |
* The HTTP method to use, eg. Get, Post. | |
* @param array $parameters | |
* The query parameters. | |
* @param string|resource|null $content | |
* The raw body data. | |
* | |
* @return string | |
* The response String. | |
* | |
* @throws \Exception | |
*/ | |
public function subRequest($path, $method = 'GET', array $parameters = [], $content = NULL) { | |
$sub_request = Request::create($path, $method, $parameters, $cookies = [], $files = [], $server = [], $content); | |
$sub_request->setSession($this->requestStack->getCurrentRequest()->getSession()); | |
$subResponse = $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST, FALSE); | |
return $subResponse->getContent(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment