adamsafr/form-request-bundle

Laravel 表单请求的 Symfony 版本适配

安装: 5,858

依赖者: 0

建议者: 0

安全性: 0

星标: 9

关注者: 2

分支: 5

公开问题: 3

类型:symfony-bundle

v0.1.2 2020-05-03 15:45 UTC

This package is auto-updated.

Last update: 2024-09-27 01:44:00 UTC


README

Build Status

表单请求包

此包提供了与 Laravel 表单请求类似的功能。表单请求是一个包含验证逻辑的自定义请求类,它在控制器动作被调用之前执行(验证)。

安装

使用 Symfony Flex 的应用程序

打开命令行,进入您的项目目录,然后执行

$ composer require adamsafr/form-request-bundle

不使用 Symfony Flex 的应用程序

步骤 1: 下载包

打开命令行,进入您的项目目录,并执行以下命令以下载此包的最新稳定版本

$ composer require adamsafr/form-request-bundle

此命令需要您已全局安装 Composer,具体请参阅 Composer 文档的安装章节

步骤 2: 启用包

然后,将包添加到项目 app/AppKernel.php 文件中注册的包列表中来启用它

// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new Adamsafr\FormRequestBundle\AdamsafrFormRequestBundle(),
        ];

        // ...
    }

    // ...
}

配置

在 Symfony 4 的 config/packages 目录中创建 adamsafr_form_request.yaml 文件,或者将其添加到 app/config/config.yml 文件中

adamsafr_form_request:
  exception_listeners:
    access_denied:
      # Sets json response of the Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
      enabled: true
    form_validation:
      # Sets json response of the Adamsafr\FormRequestBundle\Exception\FormValidationException
      enabled: true
    json_decode:
      # Sets json response of the Adamsafr\FormRequestBundle\Exception\JsonDecodeException
      enabled: true

使用

// src/Request/UserRequest.php

namespace App\Request;

use Adamsafr\FormRequestBundle\Http\FormRequest;
use App\Service\Randomizer;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints as Assert;

class UserRequest extends FormRequest
{
    /**
     * @var Randomizer
     */
    private $randomizer;

    /**
     * You can inject services here
     *
     * @param Randomizer $randomizer
     */
    public function __construct(Randomizer $randomizer)
    {
        $this->randomizer = $randomizer;
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize(): bool
    {
        return $this->randomizer->getNumber() > 0.5;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return null|Constraint|Constraint[]
     */
    public function rules()
    {
        return new Assert\Collection([
            'fields' => [
                'email' => [
                    new Assert\NotBlank(),
                    new Assert\NotNull(),
                    new Assert\Email(),
                ],
                'firstName' => new Assert\Length(['max' => 255]),
                'lastName' => new Assert\Optional([
                    new Assert\Length(['max' => 3]),
                ]),
            ],
        ]);
    }
}
// src/Controller/ApiTestController.php

namespace App\Controller;

use App\Request\UserRequest;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class ApiTestController extends AbstractController
{
    public function index(UserRequest $form)
    {
        $email = $form->getRequest()->request->get('email');
    }
}

许可证

它是在 MIT 许可证 下发布的。