search
HomeBackend DevelopmentPHP TutorialSymfoy2 HttpKernel event-driven, symfoy2httpkernel_PHP tutorial

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到
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
What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

What are the advantages of using a database to store sessions?What are the advantages of using a database to store sessions?Apr 24, 2025 am 12:16 AM

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

How do you implement custom session handling in PHP?How do you implement custom session handling in PHP?Apr 24, 2025 am 12:16 AM

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

What is a session ID?What is a session ID?Apr 24, 2025 am 12:13 AM

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

How do you handle sessions in a stateless environment (e.g., API)?How do you handle sessions in a stateless environment (e.g., API)?Apr 24, 2025 am 12:12 AM

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)