Flexible micro core for fast frameworks.
run()
method of the Symfony\Foundation\Kernel
class, you will see that after creating and saving the container in the cache or loading the cache from the DI cache, the request handler object is received and calling its handle()
method in parameters, to which the request object is passed, also received from the DI container by the way. This method should return a response object for which the send()
method is called to send a response. In general, RequestHandler assumes the first blow when processing a request, it also returns the response object, so I decided to start the overview of the components included in Symfony 2 with it.Symfony/Foundation/Resources/services.xml
since this is where the description of the RequestHandlerService service in the DI container occurs.<parameter key = "request_handler.class"> Symfony \ Components \ RequestHandler \ RequestHandler </ parameter> <parameter key = "request.class"> Symfony \ components \ RequestHandler \ Response </ parameter> ... <service id = "request_handler" class = "% request_handler.class%"> <argument type = "service" id = "event_dispatcher" /> </ service> <service id = "request" class = "% request.class%" />
new Request()
, and the request handler object as new RequestHandler($dispatcher)
, where $dispatcher
an EventDispatcher class object, which is an event dispatcher.Exception/
: directory with exception classesForbiddenHttpException.php
: exception thrown at 403 ForbiddenHttpException.php
: base class for exceptionsNotFoundHttpException.php
: exception thrown at 404 Not FoundUnauthorizedHttpException.php
: exception thrown at 401 UnauthorizedRequest.php
: request classRequestInterface.php
: an interface that must implement the request objectResponse.php
: response classResponseInterface.php
: interface that must implement the response objectRequestHandler.php
: request handlerRequest
- a request class that parses environment variables, parameters and everything related to the request. Response
- the response class that contains the body of the response, forms the headers and everything related to the response. RequestHandler
is a request handler. Perhaps consider it in more detail.core.request
core.load_controller
core.controller
core.view
core.response
core.exception
: in case of exceptionsRequestHandler
checking the application's reaction to events continues to create events or terminates. Accordingly, the application is hanging their handlers on certain events involved in the formation of a response. However, some parts of the application may not know anything about each other.core.request
event, at the same time there will be a small demonstration of the flexibility of the framework.hello/config/config.yml
file, hello/config/config.yml
comment out everything except the kernel.config: ~
line kernel.config: ~
. Now fix the application kernel, the file hello/HelloKernel.php
# hello / HelloKernel.php // remove all bundles, except for the kernel and our application public function registerBundles () { return array ( new Symfony \ Foundation \ Bundle \ KernelBundle (), new Application \ HelloBundle \ Bundle (), ); }
src/Application/HelloBundle/DependencyInjection
directory, and the HelloExtension.php
file with the following contents in it:namespace Application \ HelloBundle \ DependencyInjection; use Symfony \ Components \ DependencyInjection \ Loader \ LoaderExtension, Symfony \ Components \ DependencyInjection \ Loader \ XmlFileLoader, Symfony \ Components \ DependencyInjection \ BuilderConfiguration, Symfony \ Components \ DependencyInjection \ Reference, Symfony \ Components \ DependencyInjection \ Definition; class HelloExtension extends LoaderExtension { public function helloLoad ($ config) { $ configuration = new BuilderConfiguration (); $ loader = new XmlFileLoader (__ DIR __. '/ .. / Resources / config'); $ configuration-> merge ($ loader-> load ('hello.xml')); return $ configuration; } public function getAlias () { return 'hello'; } public function getNamespace () { return 'http://poulikov.ru/schema/dic/hello'; } public function getXsdValidationBasePath () { return __DIR __. '/ .. / Resources / config /'; } }
# src / Application / HelloBundle / Bundle.php // use our extension use Application \ HelloBundle \ DependencyInjection \ HelloExtension; ... // register the extension in the container public function buildContainer (ContainerInterface $ container) { Loader :: registerExtension (new HelloExtension ()); }
hello.xml
file along the path src/Application/HelloBundle/Resources/config/hello.xml
<container xmlns = "http://www.symfony-project.org/schema/dic/services" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd"> <parameters> <parameter key = "request_parser.class"> Application \ HelloBundle \ Request \ Parser </ parameter> </ parameters> <services> <service id = "request_parser" class = "% request_parser.class%"> <annotation name = "kernel.listener" event = "core.request" method = "resolve" /> <argument type = "service" id = "service_container" /> </ service> </ services> </ container>
core.request
to listen for the core.request
event core.request
# src / Application / HelloBundle / Request / Parser.php namespace Application \ HelloBundle \ Request; use Symfony \ Components \ DependencyInjection \ ContainerInterface; use Symfony \ Components \ EventDispatcher \ Event; use Symfony \ Components \ Routing \ RouterInterface; class parser { protected $ container; public function __construct (ContainerInterface $ container) { $ this-> container = $ container; } public function register () { $ this-> container-> getEventDispatcherService () -> connect ('core.request', array ($ this, 'resolve')); } public function resolve (Event $ event) { $ response = $ this-> container-> getResponseService (); $ response-> setContent ("<div> Hello, World! I'm Symfony 2.0 lite </ div>"); $ event-> setReturnValue ($ response); $ event-> setProcessed (true); } }
hello/config/config.yml
add the line hello.hello: ~
Source: https://habr.com/ru/post/89202/
All Articles