|
1 | 1 | HttpKernel Component
|
2 | 2 | ====================
|
3 | 3 |
|
4 |
| -HttpKernel provides the building blocks to create flexible and fast HTTP-based |
5 |
| -frameworks. |
6 |
| - |
7 |
| -``HttpKernelInterface`` is the core interface of the Symfony full-stack |
8 |
| -framework: |
9 |
| - |
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 |
| -``` |
23 |
| - |
24 |
| -It takes a ``Request`` as an input and should return a ``Response`` as an |
25 |
| -output. Using this interface makes your code compatible with all frameworks |
26 |
| -using the Symfony components. And this will give you many cool features for |
27 |
| -free. |
28 |
| - |
29 |
| -Creating a framework based on the Symfony components is really easy. Here is |
30 |
| -a very simple, but fully-featured framework based on the Symfony components: |
31 |
| - |
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 |
| -))); |
39 |
| - |
40 |
| -$request = Request::createFromGlobals(); |
41 |
| - |
42 |
| -$context = new RequestContext(); |
43 |
| -$context->fromRequest($request); |
44 |
| - |
45 |
| -$matcher = new UrlMatcher($routes, $context); |
46 |
| - |
47 |
| -$dispatcher = new EventDispatcher(); |
48 |
| -$dispatcher->addSubscriber(new RouterListener($matcher)); |
49 |
| - |
50 |
| -$resolver = new ControllerResolver(); |
51 |
| - |
52 |
| -$kernel = new HttpKernel($dispatcher, $resolver); |
53 |
| - |
54 |
| -$kernel->handle($request)->send(); |
55 |
| -``` |
56 |
| - |
57 |
| -This is all you need to create a flexible framework with the Symfony |
58 |
| -components. |
59 |
| - |
60 |
| -Want to add an HTTP reverse proxy and benefit from HTTP caching and Edge Side |
61 |
| -Includes? |
62 |
| - |
63 |
| -```php |
64 |
| -$kernel = new HttpKernel($dispatcher, $resolver); |
65 |
| - |
66 |
| -$kernel = new HttpCache($kernel, new Store(__DIR__.'/cache')); |
67 |
| -``` |
68 |
| - |
69 |
| -Want to functional test this small framework? |
70 |
| - |
71 |
| -```php |
72 |
| -$client = new Client($kernel); |
73 |
| -$crawler = $client->request('GET', '/hello/Fabien'); |
74 |
| - |
75 |
| -$this->assertEquals('Fabien', $crawler->filter('p > span')->text()); |
76 |
| -``` |
77 |
| - |
78 |
| -Want nice error pages instead of ugly PHP exceptions? |
79 |
| - |
80 |
| -```php |
81 |
| -$dispatcher->addSubscriber(new ExceptionListener(function (Request $request) { |
82 |
| - $msg = 'Something went wrong! ('.$request->get('exception')->getMessage().')'; |
83 |
| - |
84 |
| - return new Response($msg, 500); |
85 |
| -})); |
86 |
| -``` |
87 |
| - |
88 |
| -And that's why the simple looking ``HttpKernelInterface`` is so powerful. It |
89 |
| -gives you access to a lot of cool features, ready to be used out of the box, |
90 |
| -with no efforts. |
| 4 | +The HttpKernel component provides a structured process for converting a Request |
| 5 | +into a Response by making use of the EventDispatcher component. It's flexible |
| 6 | +enough to create a full-stack framework (Symfony), a micro-framework (Silex) or |
| 7 | +an advanced CMS system (Drupal). |
91 | 8 |
|
92 | 9 | Resources
|
93 | 10 | ---------
|
94 | 11 |
|
95 |
| -You can run the unit tests with the following command: |
96 |
| - |
97 |
| - $ cd path/to/Symfony/Component/HttpKernel/ |
98 |
| - $ composer install |
99 |
| - $ phpunit |
| 12 | + * [Documentation](https://symfony.com/doc/current/components/http_kernel/index.html) |
| 13 | + * [Contributing](https://symfony.com/doc/current/contributing/index.html) |
| 14 | + * [Report issues](https://github.com/symfony/symfony/issues) and |
| 15 | + [send Pull Requests](https://github.com/symfony/symfony/pulls) |
| 16 | + in the [main Symfony repository](https://github.com/symfony/symfony) |
0 commit comments