-
-
Save Daveiano/53d33d84c65fc44a23e2e63cc008f9d1 to your computer and use it in GitHub Desktop.
Drupal 8: how to create a sub-request
This file contains hidden or 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 | |
/** | |
* @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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing this, it was very helpful in working out some issues I had with my own subrequest implementation.