On Github palantirnet / slides-d8-mod-dev
Presented by Larry Garfield / Ken Rickard
interface HttpKernelInterface {
const MASTER_REQUEST = 1;
const SUB_REQUEST = 2;
/**
* Handles a Request to convert it to a Response.
*
* @param Request $request A Request instance
* @param integer $type The type of the request
* @param Boolean $catch Whether to catch exceptions or not
*
* @return Response A Response instance
*/
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true);
}
(The concept formerly known as page callbacks and now represented by a PHP callable...)
use Symfony\Component\HttpFoundation\Response;
class MyControllers {
public function hello() {
return new Response('<html><body>Hello World</body></html>');
}
}
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
class MyControllers {
public function hello() {
return new Response('<html><body>Hello World</body></html>');
}
public function helloJson() {
$data['Hello'] = 'World';
return new JsonResponse($data);
}
}
use Symfony\Component\HttpFoundation\StreamedResponse;
class MyControllers {
public function helloStream() {
$response = new StreamedResponse();
$response->setCallback(function () {
echo 'Hello World';
flush();
sleep(2);
echo 'Hello Universe';
flush();
});
return $response;
}
}
Segue: The most common example is just returning a file off disk...
use Symfony\Component\HttpFoundation\StreamedResponse;
class MyControllers {
public function helloCsv() {
$lots_of_data = get_lots_of_data();
$response = new StreamedResponse();
$response->headers->set('Content-Type', 'text/csv');
$response->setCallback(function() use ($lots_of_data) {
foreach ($lots_of_data as $record) {
print implode(', ', $record) . PHP_EOL;
}
});
return $response;
}
}
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class MyControllers {
public function helloFile() {
$response = new BinaryFileResponse('hello_world.png');
// Do this in settings.php if you know you're on nginx or
// have the Apache module enabled.
Response::trustXSendfileTypeHeader();
return $response;
}
}
Drupalers wrote that. :-)
use Symfony\Component\HttpFoundation\Response;
class MyControllers {
public function helloEmpty() {
$this->doSomething();
$response = new Response();
$response->setStatusCode(204);
return $response;
}
}
class MyControllers {
public function helloString() {
return "The view event will turn this into a response.";
}
}
class MyControllers {
public function helloDrupal() {
return array(
'#theme' => 'a_drupal_render_array',
'#description' => 'Those still exist.',
);
}
}
class MyControllers {
public function helloDrupal($to, $from, Request $request) {
return array(
'#theme' => 'love_letter',
'#from' => $from,
'#to' => $to,
);
}
}
hello.world:
path: '/hello/world/{from}/{to}'
defaults:
_content: '\Drupal\mymodule\Controller\HelloController::helloWorld'
requirements:
_permission: 'access content'
from: \s+
to: \s+
As a college campus I want to show a blast message on my site So that I can keep the campus notified in an emergency
Follow along at home:https://github.com/palantirnet/emergency_block
For reference:https://github.com/palantirnet/emergency_block
Larry Garfield, Senior Architect
Ken Rickard, Director of Development
Making the Web a Better Place
Keep tabs on our work at @Palantir
Want to hear about what we're doing?