@@ -7,17 +7,19 @@ frameworks.
7
7
`` HttpKernelInterface `` is the core interface of the Symfony2 full-stack
8
8
framework:
9
9
10
- interface HttpKernelInterface
11
- {
12
- /**
13
- * Handles a Request to convert it to a Response.
14
- *
15
- * @param Request $request A Request instance
16
- *
17
- * @return Response A Response instance
18
- */
19
- function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true);
20
- }
10
+ ``` php
11
+ interface HttpKernelInterface
12
+ {
13
+ /**
14
+ * Handles a Request to convert it to a Response.
15
+ *
16
+ * @param Request $request A Request instance
17
+ *
18
+ * @return Response A Response instance
19
+ */
20
+ function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true);
21
+ }
22
+ ```
21
23
22
24
It takes a `` Request `` as an input and should return a `` Response `` as an
23
25
output. Using this interface makes your code compatible with all frameworks
@@ -27,53 +29,61 @@ free.
27
29
Creating a framework based on the Symfony2 components is really easy. Here is
28
30
a very simple, but fully-featured framework based on the Symfony2 components:
29
31
30
- $routes = new RouteCollection();
31
- $routes->add('hello', new Route('/hello', array('_controller' =>
32
- function (Request $request) {
33
- return new Response(sprintf("Hello %s", $request->get('name')));
34
- }
35
- )));
32
+ ``` php
33
+ $routes = new RouteCollection();
34
+ $routes->add('hello', new Route('/hello', array('_controller' =>
35
+ function (Request $request) {
36
+ return new Response(sprintf("Hello %s", $request->get('name')));
37
+ }
38
+ )));
36
39
37
- $request = Request::createFromGlobals();
40
+ $request = Request::createFromGlobals();
38
41
39
- $context = new RequestContext();
40
- $context->fromRequest($request);
42
+ $context = new RequestContext();
43
+ $context->fromRequest($request);
41
44
42
- $matcher = new UrlMatcher($routes, $context);
45
+ $matcher = new UrlMatcher($routes, $context);
43
46
44
- $dispatcher = new EventDispatcher();
45
- $dispatcher->addSubscriber(new RouterListener($matcher));
47
+ $dispatcher = new EventDispatcher();
48
+ $dispatcher->addSubscriber(new RouterListener($matcher));
46
49
47
- $resolver = new ControllerResolver();
50
+ $resolver = new ControllerResolver();
48
51
49
- $kernel = new HttpKernel($dispatcher, $resolver);
52
+ $kernel = new HttpKernel($dispatcher, $resolver);
50
53
51
- $kernel->handle($request)->send();
54
+ $kernel->handle($request)->send();
55
+ ```
52
56
53
57
This is all you need to create a flexible framework with the Symfony2
54
58
components.
55
59
56
60
Want to add an HTTP reverse proxy and benefit from HTTP caching and Edge Side
57
61
Includes?
58
62
59
- $kernel = new HttpKernel($dispatcher, $resolver);
63
+ ``` php
64
+ $kernel = new HttpKernel($dispatcher, $resolver);
60
65
61
- $kernel = new HttpCache($kernel, new Store(__DIR__.'/cache'));
66
+ $kernel = new HttpCache($kernel, new Store(__DIR__.'/cache'));
67
+ ```
62
68
63
69
Want to functional test this small framework?
64
70
65
- $client = new Client($kernel);
66
- $crawler = $client->request('GET', '/hello/Fabien');
71
+ ``` php
72
+ $client = new Client($kernel);
73
+ $crawler = $client->request('GET', '/hello/Fabien');
67
74
68
- $this->assertEquals('Fabien', $crawler->filter('p > span')->text());
75
+ $this->assertEquals('Fabien', $crawler->filter('p > span')->text());
76
+ ```
69
77
70
78
Want nice error pages instead of ugly PHP exceptions?
71
79
72
- $dispatcher->addSubscriber(new ExceptionListener(function (Request $request) {
73
- $msg = 'Something went wrong! ('.$request->get('exception')->getMessage().')';
80
+ ``` php
81
+ $dispatcher->addSubscriber(new ExceptionListener(function (Request $request) {
82
+ $msg = 'Something went wrong! ('.$request->get('exception')->getMessage().')';
74
83
75
- return new Response($msg, 500);
76
- }));
84
+ return new Response($msg, 500);
85
+ }));
86
+ ```
77
87
78
88
And that's why the simple looking `` HttpKernelInterface `` is so powerful. It
79
89
gives you access to a lot of cool features, ready to be used out of the box,
0 commit comments