加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – symfony 3控制器中的自定义注释

发布时间:2020-12-13 13:17:18 所属栏目:PHP教程 来源:网络整理
导读:所以问题非常简单.我的控制器中的代码已经变得冗余,我决定为它做一个注释. if (!$request-getContentType() === 'json' ) { return new JsonResponse(array('success' = false)); } $content = $request-getContent(); if(empty($content)){ throw new BadRe
所以问题非常简单.我的控制器中的代码已经变得冗余,我决定为它做一个注释.
if (!$request->getContentType() === 'json' ) {
        return new JsonResponse(array('success' => false));
    }
    $content = $request->getContent();

    if(empty($content)){
        throw new BadRequestHttpException("Content is empty");
    }
    $data = json_decode($content,true);
    if(empty($data) || !array_key_exists('type',$data)) {
        return new JsonResponse(array('success' => false));
    }

如何制作自定义注释@CheckRequest,我可以使用$request对象作为参数?

您需要创建一个自定义注释,然后是一个注入注释阅读器并处理kernel.controller事件的侦听器:

注解

/**
 * @Annotation
 */
class CheckRequest
{
}

服务定义

services:
    controller_check_request:
        class: AppBundleEventListenerControllerCheckRequestListener
        tags:
            - { name: kernel.event_listener,event: kernel.controller,method: onKernelController}
        arguments:
            - "@annotation_reader"

监听器:

namespace AppBundleEventListener;

use AppBundleAnnotationCheckRequest;
use DoctrineCommonAnnotationsReader;
use SymfonyComponentHttpFoundationJsonResponse;
use SymfonyComponentHttpKernelEventFilterControllerEvent;
use SymfonyComponentHttpKernelExceptionBadRequestHttpException;

class ControllerCheckRequestListener
{
    /** @var Reader */
    private $reader;

    /**
     * @param Reader $reader
     */
    public function __construct(Reader $reader)
    {
        $this->reader = $reader;
    }

    /**
     * {@inheritdoc}
     */
    public function onKernelController(FilterControllerEvent $event)
    {
        if (!is_array($controllers = $event->getController())) {
            return;
        }

        $request = $event->getRequest();
        $content = $request->getContent();

        list($controller,$methodName) = $controllers;

        $reflectionClass = new ReflectionClass($controller);
        $classAnnotation = $this->reader
            ->getClassAnnotation($reflectionClass,CheckRequest::class);

        $reflectionObject = new ReflectionObject($controller);
        $reflectionMethod = $reflectionObject->getMethod($methodName);
        $methodAnnotation = $this->reader
            ->getMethodAnnotation($reflectionMethod,CheckRequest::class);

        if (!($classAnnotation || $methodAnnotation)) {
            return;
        }

        if ($request->getContentType() !== 'json' ) {
            return $event->setController(
                function() {
                    return new JsonResponse(['success' => false]);
                }
            );
        }

        if (empty($content)) {
            throw new BadRequestHttpException('Content is empty');
        }

        $data = json_decode($content,true);

        if ($request->getContentType() !== 'json' ) {
            return $event->setController(
                function() {
                    return new JsonResponse(['success' => false]);
                }
            );
        }
    }
}

请注意,不是返回响应,而是使用$event-> setController();设置整个控制器,并且在进行该调用时也必须返回.

然后在您的控制器中,您可以在整个类中设置它:

use AppBundleAnnotationCheckRequest;

/**
 * @CheckRequest
 */
class YourController extends Controller
{
}

或个别方法/行动:

use AppBundleAnnotationCheckRequest;

class TestController extends Controller
{
    /**
     * @Route("/",name="index")
     * @CheckRequest
     */
    public function indexAction(Request $request)
    {
        // ...
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读