In this short session we will look at one of the Symfony2 components adopted since Drupal 8; the HttpKernelInterface.
Attendees will gain familiarity with this component, the related Request and Response objects, and the overall top level structure of a Drupal 8 project's integration.
We will also see how the Stack PHP project allows us to easily integrate a Drupal 8 site with another codebase that uses the HttpKernelInterface (including Symfony2, Silex, Laravel or even another Drupal 8 project).
We will also explore some of the other functionality that can be added by Stack, all enabled by this powerful abstraction.
3. ”SYMFONY2 IS A REUSABLE SET OF STANDALONE,
DECOUPLED, AND COHESIVE PHP COMPONENTS
THAT SOLVE COMMON WEB DEVELOPMENT
PROBLEMS.”
– F A B I E N P O T E N C I E R , 2 0 1 1
5. SYMFONY2 ADOPTION
Driven by Components
Out of 4994 packages that depend on Symfony:
!
1911 depend on the symfony/framework-bundle,
!
1985 depend on specific components,
!
1098 depend on symfony/symfony.
http://zalas.eu/symfony2-adoption/
18. WRITING A MIDDLEWARE
class DrupalConPromoter implements HttpKernelInterface
{
private $innerKernel;
!
public function __construct(HttpKernelInterface $kernel)
{
$this->innerKernel = $kernel;
}
!
public function handle(
Request $request,
$type = HttpKernelInterface::MASTER_REQUEST, $catch = true
)
{
$response = $this->innerKernel->handle($request, $type, $catch);
$newContent = str_replace(
'<body>', '<body><h1>DRUPALCON ROCKS</h1>',
$response->getContents());
$response->setContents($newContent);
!
return $response;
}
}
19. USING OUR OWN MIDDLEWARE
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
$kernel = new AppCache($kernel);
!
$kernel = new DrupalConPromoter($kernel);
!
$request = Request::createFromGlobals();
!
$response = $kernel->handle($request);
!
$response->send();
20. AVAILABLE MIDDLEWARES
CookieGuard - Encrypts/decrypts cookies
!
GeoIP - Looks up country from IP and adds to Request
!
CORS - Adds CORS headers
!
Negotiation - Works out what content-type to send client
!
IpRestrict - block blacklisted IP Requests
!
Backstage - displays maintenance page if one exists
!
+ plenty more!
21. USING IN DRUPAL
Problem:
’Edit index.php’ is not something a typical Drupal
developer is happy with
!
Solution:
Middlewares are registered via the Service Container
(another Symfony component)
<service id=“my_drupalcon_promoter”
class=“DrupalConPromoter”>"
" <tag name=“http_middleware” priority=“1” />"
</service>