How does symfony2 work internally and how can we expand it?
From the external perspective, the symfony2 code is composed of many independent layers, each of which is based on the previous layer. Automatic loading is not directly managed by the framework. It is completed independently with the help of the universalclassloader class and src/autoload. php file.
Httpfoundation Components
The deepest level is the httpfoundation component, which provides the main objects required to process HTTP. Is an object-oriented abstraction of some PHP functions and variables.
Including:
The request class abstracts the main global variables $ _ Get, $ _ post, $ _ cookie, $ _ files, and $ _ server in PHP.
Response class, abstract class some PHP functions such as header (), setcookie () and echo ();
The session class and sessionstorageinterface interfaces Abstract The session management session _ * () function.
Httpkernel component:
A component created on the httpfoundation component that processes the dynamic part of HTTP. It is a minimum encapsulation of the request and response objects to process requests in a standard way. It also provides some extensions and tools to make it the ideal starting point for creating a Web framework.
In addition, the dependency injection component and powerful plug-in system bundles allow it to increase configuration and scalability.
Frameworkbundle bundle
Frameworkbundle bundle is a bundle, which is the main component and class library of the lightweight and fast MVC framework.
Kernel
The httpkernel class is the central class of symfony2, which processes client requests. Its main task is to convert the request object to the response object. Each symfony2 core implements the httpkernelinterface interface.
function handle(Request $request,$type=self::MASTER_REQUEST,$catch=true)
Controller
Use the Controller to convert requests to response. Controller can be any valid PHP call. The kernel delegate selects which controller should be executed to the implementer of the controllerresolverinterface interface.
public function getController(Request $request);public function getArguments(Request $request,$controller);
The getcontroller () method returns a controller (a PHP call) relative to the given request ). The default Implementation of controllerresolver is to find the _ controller attribute of the request. Its value is a string in the class: Method format. For example, bundle \ blogbundle \ postcontroller: indexaction. By default, routerlistener is used to define the request attribute _ controller. The getarguments () method returns an array of input parameters to be passed to the Controller for calling. By default, these input parameters are automatically obtained based on the request property.
Match the input parameter of the Controller method from the request property: symfony2 indicates that the input parameter of each method is used to find the property with the same name from the request. If no parameter is defined, the default value of the input parameter is used.
// Symfony2 search for the 'id' attribute (mandatory) from the request attribute // and an 'admin' attribute (optional) Public Function showaction ($ id, $ admin = true) {//...}
Request Processing: The handle () method requires a request parameter and must always return a response. To convert a request, handle () relies on a analyzer and an event notification sequence chain.
1. Before processing anything, the kernel. Request event will be notified-if a listener returns a response, it will jump directly to step 1.
2. The analyzer is called to determine which controller will be executed.
3. The kernel. Controller event listener now processes controller calls (change it and encapsulate it ...)
4. Check whether the controller is a valid PHP callback.
5. The analyzer is called to determine the parameters passed to the Controller.
6. kernel calls Controller
7. If the Controller does not return a response object, the kernel. view event listener converts the Controller return value into a response.
8. The kernel. RESPONSE event listener starts to process response (content and header );
9. The response object is returned.
If an exception is thrown during this process. exception will be notified, the listener converts the exception to a response, and then the kernel. the response event will be notified. If the conversion fails, the exception will be thrown.
If you do not want to capture exceptions, you can pass a false value as the third parameter to the handle () method to close the kernel. Exception event.
Internal request
A subrequest can be processed at any time when a primary request is processed. You can pass a request type to the handle () method as its second parameter.
HttpKernelInterface::MASTER_ReQUEST;HttpKernelInterface::SUB_REQUEST
These types are passed to all events and listeners as needed.
Event
Each event thrown by the kernel is a subclass of the kernelevent class. This means that each event can access the same basic information.
Getrequesttype () The type of the returned request (httpkernelinterface: master_request or httpkernelinterface: sub_request;
Getkernel () returns the Kernel used to process the request.
Getrequest () returns a currently processed request.
The getrequesttype () method allows the listener to know the request type. For example, if a listener must be activated by a primary request, you can add the code to the beginning of your listener method:
Use symfony \ component \ httpkernel \ httpkernelinterface; If (httpkernelinterface: master_request! ==$ Event-> getrequesttype () {// return immediately ;}
Kernel. Request event
Event class: getresponseevent
The goal of this event is to immediately return a response object or create a variable that can be called by the Controller after the event ends. Any listener can return a response object through the setresponse () method of the event. When a response object is returned, other listeners cannot be called.
Frameworkbundle uses events to publish a _ controller request attribute through routerlistener.
Requestlistener uses a routerinterface object to match the request, and determines which controller name will be stored in the request attribute of _ controller.
Kernel. Controller event:
Event class: filtercontrollerevent
Frameworkbundle does not use this event, but this event can be used as an entry point for modifying the Controller to be executed.
Use symfony \ component \ httpkernel \ event \ filtercontrollerevent; Public Function onkernelcontroller (filtercontrollerevent $ event) {$ controller = $ event-> getcontroller ();//... // here, the controller can be changed to any PHP callback function $ event-> setcontroller ($ Controller );}
Kernel. view event
Event class: getresponseforcontrollerresultevent
Frameworkbundle does not use this event, but it is used to implement a view subsystem. This event is called only when the controller cannot return a response object. The objective is to convert the returned values of other types into a response.
The Controller return value can be accessed through the getcontrollerresult method:
Use symfony \ component \ httpkernel \ event \ getresponseforcontrollerresultevent; Use symfony \ component \ httpfoundation \ response; Public Function onkernelview (events $ event) {$ val = $ event-> getcontrollerresult (); $ response = new response (); // customize response $ event-> setresponse ($ response) through return values );}
Kernel. RESPONSE event
Event class: filterresponseevent
The purpose of this event is to allow other systems to modify or replace the response object after it is created.
Public Function onkernelresponse (filterresponseevent $ event) {$ response = $ event-> getresponse (); // modify the response object}
Frameworkbundle registers many listeners:
Profilerlistener collects data from the current request
Webdebugtoolbarlistener injection web debugging Toolbar
Responselistener sets Content-Type for response based on the request format
Esilistener when response needs to parse the ESI tag, add a surrogate-control HTTP header to it.
Kernel. Exception event:
Event class: getresponseforexceptionevent
Frameworkbundle registers an exceptionlistener to direct the request to a given contoller. The listener of this event can create and set a response object, and create a new exception object or do nothing.
Use symfony \ component \ httpkernel \ event \ getresponseforexceptionevent; Use symfony \ component \ httpfoundation \ response; Public Function onkernelexception (getresponseforexceptionevent $ event) {$ exception = $ event-> getexception (); $ response = new response (); // create a response object based on caught exceptions $ event-> setresponse ($ response ); // you can create a new exception instead of the original one. // $ exception = new \ exception ('some special exception '); // $ event-> setexception ($ exception );}
Summary: We understand the main components and some main event interfaces in symfony2. We can define corresponding listener processing in each event interface to intervene in the request processing process.
URL: http://symfony.com/doc/current/book/internals.html