Home  >  Article  >  Backend Development  >  Symfoy2 HttpKernel event-driven, symfoy2httpkernel_PHP tutorial

Symfoy2 HttpKernel event-driven, symfoy2httpkernel_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:16:29993browse

Symfoy2 HttpKernel event-driven, symfoy2httpkernel

HttpKernel: event-driven ​ Symfony2 The work of the framework layer and application layer is completed in the HttpKernel::handle() method. The internal implementation of HttpKernel::handle() is actually through scheduling This is accomplished by event (event listener in HttpKernel), which is equivalent to integrating all components into a complete application. ​ Using HttpKernel is very simple, just create an EventDispatcher (event dispatcher) and controller resolver (Controller resolver), which can implement more event listeners to enrich application functions: Symfoy2 HttpKernel event-driven, symfoy2httpkernel_PHP tutorialkernel.request Event The purpose of implementing the kernel.request event is to add more information to the Request object, or to get the returned Response object (for example: obtained from the cache or the security layer denies access) The kernel.request event is the first event dispatched by HttpKernel::handle(), then multiple listeners listening to the event will be executed. Symfoy2 HttpKernel event-driven, symfoy2httpkernel_PHP tutorialThere are many types of event listeners, and their behaviors are different. For example, when the security listener determines that the user does not have sufficient permissions, a RedirectResponse object will be generated. If the Response object is currently returned directly, the kernel will be executed directly. .response event: Symfoy2 HttpKernel event-driven, symfoy2httpkernel_PHP tutorialThe purpose of the event is either to directly create and return the Response object, or to add more information to the Request object.

<span>RouterListener是Symfony框架中实现</span>kernel.request<span>事件的最重要监听器,</span>RouterListener在路由层中执行,返回一个包含符合当前请求的路由信息的数组,例如路由匹配模式里面的_controller和请求的参数({name})。这些信息都会存放在Request里的attributes数组里面,目前只是会添加路由信息到Request对象中还没有做其它的动作,但是解析Controller的时候会被用到。
        

2) Resolve the Controller

Assuming that the Response object is not created and returned when the kernel.request event is implemented, the next step is to determine and parse the controller and the parameters required by the controller. The controller part is the last bastion of the application layer and is responsible for creating and returning Response objects containing a specific page. How to determine the requested controller depends entirely on the application. This work is completed by the controller resolver - a class that implements ControllerResolverInterface and is also a parameter of the HttpKernel constructor. Symfoy2 HttpKernel event-driven, symfoy2httpkernel_PHP tutorialFirst call the getController() method of the controller resolver and pass in the Request object to this method. The controller resolver determines and returns the controller based on the information contained in the Request. The second method, getArguments() will be executed when the kernel.controller event is scheduled. ​

<span><em>解析Controller</em></span>
<span><em>Symfony框架使用内置的ControllerResolver(实质上是使用了一个有额外的功能的子类),该解析器利用了RouterListener保存到Request对象的attributes属性里信息来确定controller。</em></span>
<span><em> </em></span>
<span><em>getController</em></span>
<em>ControllerResolver </em><em>在Request对象的attributes数组中</em><em>查找 _controller 键(这些信息实际上是由RouterListener存放进Request对象中的):</em>
<em> </em>
<em>a) 如果</em><em><span><span>_controller 键对应</span></span></em><em><span>AcmeDemoBundle:Default:index 这个格式的值,那么该值就包含了类名和方法名,可以被Symfony框架解析成为,例如:</span></em><em><span>Acme\DemoBundle\Controller\DefaultController::indexAction,这个转换是由Symfony框架的特定的</span></em><em>ControllerResolver的子类完成的。</em>
<em> </em>
<em>b) 你的controller类会被实例化,而且该controller类必须包含一个无参的构造函数。</em>
<em> </em>
<span><em>c) 如果你的controller还实现了ContainerAwareInterface,那么setContainer方法就会被调用,container就会被注入到controller中,</em></span><em><span>这个实现也是由Symfony框架的特定的</span></em><em>ControllerResolver的子类完成的。</em>
<em> </em>




<em><span>上面也有一些其他变化过程,例如你把你的controller配置成为service。</span></em>
<em><span> </span></em>
<em>      </em>





3) The <code><code><code><span>kernel.controller</span>kernel.controllerEvent

The kernel.controller event is to initialize some information or change the controller object before the controller is executed.
After the called controller is determined, HttpKernel::handle() will dispatch the

kernel.controller event. After certain parts of the system are determined (for example: controller, routing information, etc.) but before these parts are executed, the listener for the kernel.controller event will run. Symfoy2 HttpKernel event-driven, symfoy2httpkernel_PHP tutorial 4) Get controller parameters Symfoy2 HttpKernel event-driven, symfoy2httpkernel_PHP tutorial The getAttributes() method returns a parameter array, which will be passed to the controller. We can also customize this method, or use the built-in Symfony framework. ControllerResolver uses the radiation mechanism to obtain the parameter list of the called controller method. Iterate through the list and use the following steps to determine the one-to-one values ​​in the parameter list:
<code><code><span> </span></code></code>
<span><em>a) 使用参数作为键查找Request对象中的attributes数组,如果找到,那么相应的值会传入到controller的方法中,例如:controller方法的第一个参数是$name,那么在Request的attributes数组中包含$</em></span><em>attributes['name']的值,那么</em><span><em>$</em></span><em>attributes['name']就会被使用。</em>
<em> </em>
<em>b) 如果该该参数在Symfony配置routing的时候被指定,那么就会跳过对该参数的查找。</em>
<em> </em>


5) Call controller

        这一步,controller就会被执行。         controller会创建包含特定页面或者json的Response对象,这也是应用层的最后一个步骤。  Symfoy2 HttpKernel event-driven, symfoy2httpkernel_PHP tutorial  Symfony框架中没有缺省的监听器实现kernel.view事件,可是,有一个核心Bundle——SensioFrameworkExtraBundle里有个监听改事件的监听器。如果你的controller返回一个数组,并且在controller类的顶部有@Template的注解,那么该监听器就会渲染一个模板,把controller返回的数组传入到模板中,最后利用模板返回的内容创建一个Response对象,并返回该Response对象。   除此之外,FOSRestBundle也实现了监听该事件的监听器,a listener on this event which aims to give you a robust view layer capable of using a single controller to return many different content-type responses (e.g. HTML, JSON, XML, etc).             7) kernel.response 事件       在发送Response对象到客户端前修改它。                 kernel的目的是把Request对象转换成为Response对象。Response对象可能是在kernel.request事件中创建,可能是由controller返回,又或者是由监听kernel.view事件的监听器返回。         不管是在哪一个环节创建Response对象,最后kernel.response事件都会被触发。监听kernel.response事件的监听器都会以某种方式修改Response对象,例如:修改Response的header部分,修改cookie,或者甚至会修改Response对象返回的内容(注入javascript到36cc49f0c466276486e50c850b7e4956标签前等等)          kernel.response事件完成后,HttpKernel::handle()返回最终的Response对象,调用Response::send()箱客户端发送headers头部和Response实体。  
Symfony框架实现kernel.response事件
<em> </em>
Symfony框架内置几个监听器监听kernel.response事件,更多的可以通过开发者社区获得。例如:在dev开发环境下WebDebugToolbarListener向页面的底部注入javascript代码,debug工具条就会显示出来。还有另一个监听器,ContextListener序列化当前用户的信息保存到session中,下一次请求的时候直接在session中重载用户信息。

   

        8) kernel.terminate事件

The listener that listens to this event usually handles some time-consuming background programs. ​ The last event of the HttpKernel process is the kernel.terminate event, and the event is triggered after the HttpKernel::handle() method, and the response content has been sent to the user. Symfoy2 HttpKernel event-driven, symfoy2httpkernel_PHP tutorial Symfony framework a complete workflow ​ When using the HttpKernel component, we do not need to implement any listeners to add to kernel events, nor do we need to implement a controller resolver. The listener and controller resolver that come with the HTTP component can work normally: Symfoy2 HttpKernel event-driven, symfoy2httpkernel_PHP tutorial Sub-request ​ In addition to passing the "main request" into HttpKernel::handle, you can also pass the so-called "sub request" into HttpKernel::handle. Subrequests look similar to other requests. The difference is that a general request renders a complete page, while a subrequest renders a part of a page. Usually we create a subrequest in the controller (or create it in the template). ​ When the HttpKernel::handle method runs a subrequest, you need to modify the value of the second parameter: A subrequest also creates a complete request-response cycle. The only difference is that some listeners may only run in the "main request" (security). The subclass of KernelEvent is passed to the listener, and the listener determines whether the current request is "main request" or "sub request" through KernelEvent::getRequestType(). For example, a listener will only be executed under the request of "main request":

What is the difference between the kernels of linux24 and linux26? Maybe some drivers have been added? Are there drivers for all hardware in there? For example USB

It’s not your fault that you don’t know this. Your fault is that you always want to wait for others to bring you ready-made things. Find out for yourself! ~Look slowly so you can grow. Stop wasting your time asking these pointless questions.

Lenovo G450, newly installed win81, the driver is installed with driver life, automatic restart often occurs, check the event is kernel-power

Dear Lenovo users!
This is caused by missing or incomplete files or conflicts with other software.
It is recommended that you download the graphics card driver from the official website, link:
support1.lenovo.com.cn/lenovo/wsi/Modules/Drive.aspx
It is recommended that you go to Control Panel - Device Manager before uninstalling Uninstall the corresponding driver, then boot up and press F8 to enter safe mode for installation.
If it doesn’t work, you can try win+r, enter msconfig, and close the startup items to troubleshoot conflicting software programs.
Or enter cmd and enter sfc/scannow at the command prompt to repair system files.
The problem is still not solved, it is recommended that you reinstall the system.
You can consult for more questions
idea forum: lenovobbs.lenovo.com.cn/...ureply
Think forum: thinkbbs.lenovo.com.cn/...ureply
Lenovo Music Community: bbs.lenovomobile.com/...ureply
Looking forward to your satisfactory evaluation, thank you for your support of Lenovo, and wish you a happy life!
Lenovo Enterprise Platform [Official certification]

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/897400.htmlTechArticleSymfoy2 HttpKernel event-driven, symfoy2httpkernel HttpKernel: event-driven Symfony2 framework layer and application layer work are all in HttpKernel:: Completed in the handle() method, HttpKernel::ha...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn