Symfony 验证缓慢 [英] Symfony validation slow

查看:46
本文介绍了Symfony 验证缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚我在 symfony 中做错了什么来验证请求.

I'm having a hard time figuring out what I am doing wrong in symfony to validate a request.

我有一个通过 POST 请求接收用户的控制器

I have a controller which receives a User via a POST request

public function createAction(Request $request)
{
    $params = $request->request->all();
    $user = new User();
    $user->setUsername($params['username']);
    $user->setEmail($params['email']);
    $user->setPassword($params['password']);

    $errors = $this->get('validator')->validate($user);

    if(count($errors) > 0)
    {
        return $this->respond(['errors' => $errors], 422);
    }
    else 
    {
        $user = $this->get('userrepository')->create($user);
        return $this->respond($user, 201);
    }
}

如您所见,我首先创建用户,然后将其传递给验证器服务并返回错误(如果有),否则我会要求我的存储库在数据库中创建用户.

As you can see, I first create the User, then pass it to the validator service and return the errors if any, otherwise I ask my repository to create the user in the database.

我使用的验证如下(YAML):

The validation I use is the following (YAML):

AppBundle\Entity\User:
    constraints:
        - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: email
    properties:
        username:
            - NotBlank: ~
        email:
            - NotBlank: ~
            - Email:
                message: The email {{ value }} is not a valid email.
        password:
            - NotBlank: ~

现在验证工作了,但是 Symfony 需要大约 3000 毫秒才能在一个带有空数据库的 dev env 中做出响应.

Now the validation works, but it takes Symfony around 3000ms to respond in a dev env with an empty database.

这个荒谬响应时间长的原因是什么?

What could be the cause of this absurdly long response time ?

其他未经验证的 GET/POST 请求只需 250-300 毫秒即可完成.

Other GET/POST request without validation only take 250-300 ms to complete.

PS:我尝试将验证规则作为注释放入实体中,但响应时间没有区别.

PS: I've tried putting the validation rules into the Entity as Annotations, but there is no difference in response time.

推荐答案

供以后参考:

我的问题是使用 /web/app_dev.php/... 访问 symfony 而不是在开发中使用内置服务器.

The issue for me was using the /web/app_dev.php/... to access symfony instead of using the build in server in development.

$ php app/console server:run

这篇关于Symfony 验证缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆