rin-project/fast-crud

让 CRUD 非常简单!(尚未完成,即将推出!)

安装: 3

依赖项: 1

建议者: 0

安全性: 0

星星: 0

关注者: 1

分支: 0

公开问题: 2

类型:symfony-bundle

v0.1.0 2020-08-09 16:55 UTC

README

让我们让 CRUD 非常简单!如果您有任何有趣的点子,请记录问题。

先决条件

  • PHP 7.1.3 或更高版本
  • Symfony 5.0 或更高版本

测试环境

  • Mac OSX 10.15 Catalina
  • PHP 7.2.7
  • Symfony 5.1

安装

打开命令行,进入您的项目目录,并使用 composer 安装 fast-crud

$ composer require rin-project/fast-crud

如果您不使用 Flex,请将捆绑配置添加到 App\Kernel.php 中以注册捆绑

return [
    // ...

    // add this line
    RinProject\FastCrudBundle\FastCrudBundle::class => ['all' => true],
];

config/packages/framework.yaml 中导入默认配置

# config/packages/framework.yaml
imports:
    - { resource: "@FastCrudBundle/Resources/config/config.yaml" }

config/routes.yaml 中导入默认路由(可选)

# config/routes.yaml
fast-crud-route:
    resource: "@FastCrudBundle/Resources/config/routes.yaml"

如果您想自动捕获所有异常,请启用配置文件中的异常拦截器 config/packages/framework.yaml

# config/packages/framework.yaml
fast_crud:
    exception_interceptor:
        enabled: true
        effective_pattern: /^\/(api|manage)\/.*$/

使用方法

通过命令生成

C: create
R: retrieve
U: update
D: delete
L: list

示例

$ php bin/console make:fast-crud RUDL App:Content
$ php bin/console make:fast-crud RL App:Region

或者手动生成

如果您有实体、控制器和服务的相同 PRIMARY NAME,例如 User, UserController, UserService

创建一个 Fast-CRUD 服务

namespace App\Service;

use RinProject\FastCrudBundle\Service\CrudService;

final class UserService extends CrudService {}

创建一个继承自 CrudController 的控制器

namespace App\Api\Controller;

use RinProject\FastCrudBundle\Controller\CrudController;
use RinProject\FastCrudBundle\View\ApiView;
use RinProject\FastCrudBundle\View\Mixin\SingleCreateAndUpdateApiViewMixin;
use RinProject\FastCrudBundle\View\Mixin\SingleRetrieveApiViewMixin;

/**
 * @Route("/api/user", name="api-user-")
 */
class UserController extends CrudController
{
    use ApiView, SingleRetrieveApiViewMixin, SingleCreateAndUpdateApiViewMixin;

    public function commonFilter()
    {
        return ['id' => $this->getUser()];
    }
}

或者,如果您有不同的 PRIMARY NAME,例如 Staff, UserController 和 PersonService

服务

namespace App\Service;

use App\Entity\Staff;
use RinProject\FastCrudBundle\Service\CrudService;

final class PersonService extends CrudService
{
    function __construct(ContainerInterface $container)
    {
        parent::__construct($container, Staff::class);
    }
}

控制器

namespace App\Api\Controller;

use App\Service\PersonService;
use RinProject\FastCrudBundle\Controller\CrudController;
use RinProject\FastCrudBundle\View\ApiView;
use RinProject\FastCrudBundle\View\Mixin\SingleCreateAndUpdateApiViewMixin;
use RinProject\FastCrudBundle\View\Mixin\SingleRetrieveApiViewMixin;

/**
 * @Route("/api/user", name="api-user-")
 */
class UserController extends CrudController
{
    use ApiView, SingleRetrieveApiViewMixin, SingleCreateAndUpdateApiViewMixin;

    public function __construct()
    {
        $this->serviceClass = PersonService::class;
    }

    public function commonFilter()
    {
        return ['id' => $this->getUser()];
    }
}

待续...