thephpguys/light-crud-bundle

为您的实体创建简单的CRUD

dev-master 2020-09-22 07:56 UTC

This package is auto-updated.

Last update: 2024-09-22 16:45:05 UTC


README

请确保已全局安装Composer,具体请参考Composer文档中的安装章节

使用Symfony Flex的应用程序

打开命令行控制台,进入您的项目目录并执行

$ composer require thephpguys/light-crud-bundle

不使用Symfony Flex的应用程序

步骤1:下载Bundle

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

$ composer require thephpguys/light-crud-bundle

步骤2:启用Bundle

然后,通过将其添加到项目config/bundles.php文件中注册的Bundle列表中来启用此Bundle

// config/bundles.php

return [
    // ...
    TPG\LightCrudBundle\LightCrudBundle::class => ['all' => true],
];

入门指南

实体

定义您的实现TPG\LightCrudBundle\JsonBodySerializable接口的实体

use TPG\LightCrudBundle\JsonBodySerializable;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="posts")
 */
class Post implements JsonBodySerializable
{
    /**
     * @ORM\Id
     * @ORM\Column(type="uuid")
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     */
    public $id;


    /**
     * @ORM\Column(type="string")
     */
    public $title;
    
    /**
     * @ORM\Column(type="string")
     */
    public $description;

}

控制器

定义您的控制器

namespace App\Controller;

use App\Entity\Post;
use TPG\LightCrudBundle\Annotation\EntityClass;
use TPG\LightCrudBundle\Annotation\EntityView;
use TPG\LightCrudBundle\DataLoader\DataLoader;
use TPG\LightCrudBundle\DataLoader\LoadContext;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class UserController
 * @package App\Controller
 * @Route("/posts")
 * @EntityClass("App\Entity\Post")
 */
class PostsController
{

    /**
     * @Route("/",methods="POST")
     * @param Request $request
     */
    public function create(Post $post, EntityManagerInterface $em){
        $em->persist($post);
        $em->flush();
        return new Response(204);
    }

    /**
     * @Route("/{id}/",methods="PUT");
     */
    public function update(Post $post, EntityManagerInterface $em)
    {
        $em->flush();
        return new Response(204);
    }
    /**
     * @Route("/{id}/",methods="DELETE");
     */
    public function remove(Post $post, EntityManagerInterface $em)
    {
        $em->remove($post);
        $em->flush();
        return new Response(204);
    }

    /**
     * @Route("/")
     * @EntityView(fields={"title"})
     */
    public function list(LoadContext $context, DataLoader $dataLoader):JsonResponse
    {
        return new JsonResponse($dataLoader->loadCollection($context));
    }

    /**
     * @param string $id
     * @Route("/{id}/");
     * @EntityView(fields={"title","description"})
     */
    public function show(LoadContext $context, DataLoader $dataLoader, string $id):JsonResponse
    {
        return new JsonResponse($dataLoader->loadOne($id,$context));
    }

}

@EntityClass注解指示使用哪个实体。

@EntityView注解指示将选择哪些字段并显示。