theunic/symfony-response-helpers 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

theunic/symfony-response-helpers

最新稳定版本:v1.0.0

Composer 安装命令:

composer require theunic/symfony-response-helpers

包简介

A set of helpers intended to ease the instantiation of symfony responses

README 文档

README

A small utility to ease the instantiation of Response objects

Installation

composer req theunic/symfony-response-helpers

Usage

Use it in your Symfony controllers

<?php

declare(strict_types=1);

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\Routing\Annotation\Route;

final class HomePageController extends AbstractController
{
    #[Route("/")]
    public function __invoke(): Response
    {
        return Response\ok("<h1>Hello World!</h1>");
    }
}

Or to generate json responses

<?php

declare(strict_types=1);

namespace App\Controller\Api;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use App\Repository\UsersRepository;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Routing\Annotation\Route;

final class UsersController extends AbstractController
{
    public function __construct(
        private readonly UsersRepository $usersRepository,
        private readonly ValidatorInterface $validator,
    ) {
    }
    
    #[Route("/api/users/{id}", methods: ["PUT"])]
    public function __invoke(int $id, Request $req): JsonResponse
    {
        $user = $this->usersRepository->find($id);
        
        if (!$user) {
            return JsonResponse\notFound(["error" => sprintf("User with id %d was not found", $id)]);
        }
        
        $data = json_decode($req->getContent(), associative: true, falgs: JSON_THROW_ON_ERROR);
        $constraint = new Assert\Collection([
            'username' => new Assert\NotEmpty(),
            'email' => [new Assert\NotEmpty(), new Assert\Email()]
        ]);
        
        $errors = $this->validator->validate($data, $contraint);
        
        if (count($errors) > 0) {
            return JsonResponse\badRequest(
                array_reduce(
                    $errors,
                    static function (array $errors, ContraintViolation $cv): array {
                        $errors[$cv->getPropertyPath()] = $error->getMessage();
                        return $errors;
                    },
                    []
                )
            );
        }
        
        return JsonResponse\ok([
            "id" => $id,
            "username" => $user->getUsername(),
            "email" => $user->getEmail()
        ]);
    }
}

统计信息

  • 总下载量: 1.34k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-24