美文网首页
监听组件

监听组件

作者: 美丽的她 | 来源:发表于2017-03-16 11:22 被阅读24次

<?php

/*

  • This file is part of the Symfony package.
  • (c) Fabien Potencier fabien@symfony.com
  • For the full copyright and license information, please view the LICENSE
  • file that was distributed with this source code.
  • 有关完整的版权和许可信息,请查看随此源代码分发的LICENSE文件。
    */

namespace Symfony\Component\HttpKernel;

/**

  • Contains all events thrown in the HttpKernel component.

  • 包含HttpKernel组件中抛出的所有事件。

  • @author Bernhard Schussek bschussek@gmail.com
    /
    final class KernelEvents
    {
    /
    *

    • The REQUEST event occurs at the very beginning of request
    • dispatching.
    • REQUEST事件发生在请求分派的开始。
    • This event allows you to create a response for a request before any
    • other code in the framework is executed.
    • 他的事件允许您在框架中的任何其他代码执行之前创建一个请求的响应。
    • @Event("Symfony\Component\HttpKernel\Event\GetResponseEvent")
    • @var string
      */
      const REQUEST = 'kernel.request';

    /**

    • The EXCEPTION event occurs when an uncaught exception appears.
    • 出现未捕获异常时,发生EXCEPTION事件。
    • This event allows you to create a response for a thrown exception or
    • to modify the thrown exception.
    • 此事件允许您为抛出的异常创建响应或修改抛出的异常。
    • @Event("Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent")
    • @var string
      */
      const EXCEPTION = 'kernel.exception';

    /**

    • The VIEW event occurs when the return value of a controller
    • is not a Response instance.
    • 当控制器的返回值不是响应实例时,VIEW事件发生。
    • This event allows you to create a response for the return value of the
    • controller.
    • 此事件允许您创建控制器的返回值的响应。
    • @Event("Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent")
    • @var string
      */
      const VIEW = 'kernel.view';

    /**

    • The CONTROLLER event occurs once a controller was found for
    • handling a request.
    • 一旦找到用于处理请求的控制器,就会发生CONTROLLER事件。
    • This event allows you to change the controller that will handle the
    • request.
    • 此事件允许您更改将处理请求的控制器。
    • @Event("Symfony\Component\HttpKernel\Event\FilterControllerEvent")
    • @var string
      */
      const CONTROLLER = 'kernel.controller';

    /**

    • The CONTROLLER_ARGUMENTS event occurs once controller arguments have been resolved.
    • 一旦控制器参数已解析,就会发生CONTROLLER_ARGUMENTS事件。
    • This event allows you to change the arguments that will be passed to
    • the controller.
    • 此事件允许您更改将传递到控制器的参数。
    • @Event("Symfony\Component\HttpKernel\Event\FilterControllerArgumentsEvent")
    • @var string
      */
      const CONTROLLER_ARGUMENTS = 'kernel.controller_arguments';

    /**

    • The RESPONSE event occurs once a response was created for
    • replying to a request.
    • 一旦响应被创建用于答复请求,就会发生RESPONSE事件。
    • This event allows you to modify or replace the response that will be
    • replied.
    • 此事件允许您修改或替换将要回复的响应。
    • @Event("Symfony\Component\HttpKernel\Event\FilterResponseEvent")
    • @var string
      */
      const RESPONSE = 'kernel.response';

    /**

    • The TERMINATE event occurs once a response was sent.
    • 一旦发送响应,就会发生TERMINATE事件。
    • This event allows you to run expensive post-response jobs.
    • 此事件允许您运行昂贵的回复作业。
    • @Event("Symfony\Component\HttpKernel\Event\PostResponseEvent")
    • @var string
      */
      const TERMINATE = 'kernel.terminate';

    /**

    • The FINISH_REQUEST event occurs when a response was generated for a request.
    • 当为请求生成响应时,会发生FINISH_REQUEST事件。
    • This event allows you to reset the global and environmental state of
    • the application, when it was changed during the request.
    • 此事件允许您在请求期间更改应用程序的全局和环境状态。
    • @Event("Symfony\Component\HttpKernel\Event\FinishRequestEvent")
    • @var string
      */
      const FINISH_REQUEST = 'kernel.finish_request';
      }

第一步:处理监听类

<?php

namespace AppBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class AcmeExceptionListener
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
// You get the exception object from the received event
//您从收到的事件中获取异常对象
$exception = $event->getException();
$message = sprintf(
'My Error says: %s with code: %s',
$exception->getMessage(),
$exception->getCode()
);

    // Customize your response object to display the exception details
    //自定义响应对象以显示异常详细信息
    $response = new Response();
    $response->setContent($message);

    // HttpExceptionInterface is a special type of exception that holds status code and header details
    // HttpExceptionInterface是一种特殊类型的异常保存状态代码和标题详细信息
    if ($exception instanceof HttpExceptionInterface) {
        $response->setStatusCode($exception->getStatusCode());
        $response->headers->replace($exception->getHeaders());
    } else {
        $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
    }

    // Send the modified response object to the event
    // 将修改后的响应对象发送到事件
    $event->setResponse($response);
}

}

第二步:注入事件

-app/config/services.yml
services:
kernel.listener.your_listener_name:
class: AppBundle\EventListener\AcmeExceptionListener
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
kernel.exception 指监听整个框架所有的错误
kernel.request 指监听整个框架所有的请求前的事情
method: onKernelException 指监听某个类下的一个方法

相关文章

网友评论

      本文标题:监听组件

      本文链接:https://www.haomeiwen.com/subject/pqgdnttx.html