Skip to content

Instantly share code, notes, and snippets.

@Daveiano
Forked from dmouse/SubRequestController.php
Last active February 28, 2020 21:24

Revisions

  1. Daveiano revised this gist Feb 22, 2019. 2 changed files with 16 additions and 4 deletions.
    17 changes: 14 additions & 3 deletions SubRequestController.php
    Original file line number Diff line number Diff line change
    @@ -21,28 +21,39 @@ class SubRequestController extends ControllerBase implements ContainerInjectionI
    */
    protected $httpKernel;

    /**
    * @var \Symfony\Component\HttpFoundation\RequestStack
    */
    protected $requestStack;

    /**
    * {@inheritdoc}
    */
    public function __construct(HttpKernelInterface $http_kernel) {
    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, array $parameters = []) {
    $sub_request = Request::create($path, 'GET', $parameters);
    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);

    3 changes: 2 additions & 1 deletion call-it.php
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,8 @@
    use Drupal\content_helper\Controller\SubRequestController;

    $httpKernel = \Drupal::service('http_kernel.basic');
    $sub_request = new SubRequestController($httpKernel);
    $requestStack = \Drupal::requestStack();
    $sub_request = new SubRequestController($httpKernel, $requestStack);

    $api_response = $sub_request->subRequest(
    '/jsonapi/field_config/field_config',
  2. Daveiano revised this gist Feb 20, 2019. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions call-it.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    <?php

    use Drupal\content_helper\Controller\SubRequestController;

    $httpKernel = \Drupal::service('http_kernel.basic');
    $sub_request = new SubRequestController($httpKernel);

    $api_response = $sub_request->subRequest(
    '/jsonapi/field_config/field_config',
    [
    'filter' => [
    'bundle' => 'institution',
    'entity_type' => 'node',
    ],
    'page' => [
    'offset' => '50',
    ],
    ]
    );
  3. Daveiano revised this gist Feb 20, 2019. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions SubRequestController.php
    Original file line number Diff line number Diff line change
    @@ -44,10 +44,9 @@ public function __construct(HttpKernelInterface $http_kernel) {
    public function subRequest($path, array $parameters = []) {
    $sub_request = Request::create($path, 'GET', $parameters);

    $subResponse = $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
    $subResponseContent = $subResponse->getContent();
    $subResponse = $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST, FALSE);

    return $subResponseContent;
    return $subResponse->getContent();
    }

    }
  4. Daveiano revised this gist Feb 20, 2019. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions SubRequestController.php
    Original file line number Diff line number Diff line change
    @@ -33,17 +33,21 @@ public function __construct(HttpKernelInterface $http_kernel) {
    *
    * @param string $path
    * Path to use for subrequest.
    * @param array $parameters
    * The query parameters.
    *
    * @return string
    * The response String.
    *
    * @throws \Exception
    */
    public function subRequest($path) {
    $sub_request = Request::create($path, 'GET');
    public function subRequest($path, array $parameters = []) {
    $sub_request = Request::create($path, 'GET', $parameters);

    $subResponse = $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
    $subResponseContent = $subResponse->getContent();

    return $subResponse->getContent();
    return $subResponseContent;
    }

    }
  5. Daveiano revised this gist Feb 11, 2019. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions SubRequestController.php
    Original file line number Diff line number Diff line change
    @@ -34,17 +34,16 @@ public function __construct(HttpKernelInterface $http_kernel) {
    * @param string $path
    * Path to use for subrequest.
    *
    * @return string $html
    * @return string
    * The response String.
    *
    * @throws \Exception
    */
    public function subRequest($path) {
    $sub_request = Request::create($path, 'GET');

    $subResponse = $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
    $html = $subResponse->getContent();

    return $subResponse;
    return $subResponse->getContent();
    }

    }
  6. Daveiano revised this gist Feb 11, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion SubRequestController.php
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,6 @@
    namespace Drupal\content_helper\Controller;

    use Drupal\Core\Controller\ControllerBase;
    use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    use Symfony\Component\HttpKernel\HttpKernelInterface;
    use Symfony\Component\HttpFoundation\Request;
  7. Daveiano revised this gist Feb 11, 2019. 1 changed file with 0 additions and 9 deletions.
    9 changes: 0 additions & 9 deletions SubRequestController.php
    Original file line number Diff line number Diff line change
    @@ -29,15 +29,6 @@ public function __construct(HttpKernelInterface $http_kernel) {
    $this->httpKernel = $http_kernel;
    }

    /**
    * {@inheritdoc}
    */
    public static function create(ContainerInterface $container) {
    return new static(
    $container->get('http_kernel')
    );
    }

    /**
    * Performs a subrequest.
    *
  8. Daveiano revised this gist Feb 11, 2019. 1 changed file with 6 additions and 8 deletions.
    14 changes: 6 additions & 8 deletions SubRequestController.php
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,18 @@
    <?php

    /**
    * @file
    * Contains Drupal\dmouse\Controller\SubRequestController.
    * Generated by drupal/console.
    */

    namespace Drupal\dmouse\Controller;
    namespace Drupal\content_helper\Controller;

    use Drupal\Core\Controller\ControllerBase;
    use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    use Symfony\Component\HttpKernel\HttpKernelInterface;
    use Symfony\Component\HttpFoundation\Request;


    /**
    * Class SubRequest.
    *
    * @package Drupal\content_helper\Controller
    */
    class SubRequestController extends ControllerBase implements ContainerInjectionInterface {

    /**
  9. Daveiano revised this gist Feb 11, 2019. 1 changed file with 17 additions and 6 deletions.
    23 changes: 17 additions & 6 deletions SubRequestController.php
    Original file line number Diff line number Diff line change
    @@ -22,26 +22,37 @@ class SubRequestController extends ControllerBase implements ContainerInjectionI
    *
    * @var Symfony\Component\HttpKernel\HttpKernelInterface
    */
    protected $http_kernel;
    protected $httpKernel;

    /**
    * {@inheritdoc}
    */
    public function __construct(HttpKernelInterface $http_kernel) {
    $this->httpKernel = $http_kernel;
    }

    /**
    * {@inheritdoc}
    */
    public static function create(ContainerInterface $container) {
    return new static(
    $container->get('http_kernel')
    );
    }

    /**
    * Index.
    * Performs a subrequest.
    *
    * @param string $path
    * Path to use for subrequest.
    *
    * @return string $html
    * The response String.
    *
    * @return string
    * Return Hello string.
    * @throws \Exception
    */
    public function index() {
    $sub_request = Request::create('/search/node', 'GET');
    public function subRequest($path) {
    $sub_request = Request::create($path, 'GET');

    $subResponse = $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
    $html = $subResponse->getContent();
  10. @dmouse dmouse revised this gist Oct 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion SubRequestController.php
    Original file line number Diff line number Diff line change
    @@ -41,7 +41,7 @@ public static function create(ContainerInterface $container) {
    * Return Hello string.
    */
    public function index() {
    $sub_request = Request::create('/user', 'GET');
    $sub_request = Request::create('/search/node', 'GET');

    $subResponse = $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
    $html = $subResponse->getContent();
  11. @dmouse dmouse revised this gist Oct 6, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions SubRequestController.php
    Original file line number Diff line number Diff line change
    @@ -18,9 +18,9 @@
    class SubRequestController extends ControllerBase implements ContainerInjectionInterface {

    /**
    * Stack\StackedHttpKernel definition.
    * Symfony\Component\HttpKernel\HttpKernelInterface definition.
    *
    * @var Stack\StackedHttpKernel
    * @var Symfony\Component\HttpKernel\HttpKernelInterface
    */
    protected $http_kernel;

  12. @dmouse dmouse created this gist Oct 6, 2014.
    51 changes: 51 additions & 0 deletions SubRequestController.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    <?php

    /**
    * @file
    * Contains Drupal\dmouse\Controller\SubRequestController.
    * Generated by drupal/console.
    */

    namespace Drupal\dmouse\Controller;

    use Drupal\Core\Controller\ControllerBase;
    use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    use Symfony\Component\HttpKernel\HttpKernelInterface;
    use Symfony\Component\HttpFoundation\Request;


    class SubRequestController extends ControllerBase implements ContainerInjectionInterface {

    /**
    * Stack\StackedHttpKernel definition.
    *
    * @var Stack\StackedHttpKernel
    */
    protected $http_kernel;

    public function __construct(HttpKernelInterface $http_kernel) {
    $this->httpKernel = $http_kernel;
    }

    public static function create(ContainerInterface $container) {
    return new static(
    $container->get('http_kernel')
    );
    }

    /**
    * Index.
    *
    * @return string
    * Return Hello string.
    */
    public function index() {
    $sub_request = Request::create('/user', 'GET');

    $subResponse = $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
    $html = $subResponse->getContent();

    return $subResponse;
    }
    }