phpjuice / blueprint
Blueprint是一个强大的CRUD生成器,可加快您Laravel应用的开发速度。
Requires
- illuminate/support: ^7.0
Requires (Dev)
- mockery/mockery: ^1.4
- orchestra/testbench: 5.12.1
- phpstan/phpstan: ^0.12.64
- phpunit/phpunit: ^9.5
- sempro/phpunit-pretty-print: ^1.3
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-08-30 01:11:44 UTC
README
Blueprint是一个强大的CRUD生成器,可加快您Laravel应用的开发速度。
安装
Blueprint包需要Laravel 5.5或更高版本。
INFO: 如果您正在使用较旧的Laravel版本,此包可能无法正常工作。
安装Blueprint包的推荐方式是通过Composer。
composer require phpjuice/blueprint --dev
此包支持Laravel 5.5或更高版本的包自动发现功能,服务提供程序将自动注册。在框架的旧版本中,只需在config/app.php文件中添加服务提供程序即可。
'providers' => [ // ... PHPJuice\Blueprint\BlueprintServiceProvider::class, ];
之后,您可以使用Artisan命令vendor:publish发布其模板文件。
php artisan vendor:publish --tag=blueprint.templates
您可以使用以下命令发布配置文件:
php artisan vendor:publish --tag=blueprint.config
发布后,config/blueprint.php配置文件包含以下内容:
<?php return [ 'custom_template' => false, /* |-------------------------------------------------------------------------- | Blueprint Template Stubs Storage Path |-------------------------------------------------------------------------- | | Here you can specify your custom template path for the generator. | */ 'path' => base_path('resources/vendor/blueprint/'), ];
用法
Blueprint CRUD生成器设计得非常简单直观。您需要做的就是创建一个CRUD蓝图文件,然后生成它。
创建CRUD
为了创建蓝图文件,我们使用以下Artisan命令
php artisan blueprint:make CRUD_NAME
示例
php artisan blueprint:make Post
运行此命令后,将在您的数据库文件夹database/blueprints下生成一个CRUD蓝图文件,命名规范将遵循Laravel迁移命名规范。例如:2018_12_09_144004_create_post_crud_blueprint.json
以下是蓝图JSON文件的基本结构,根据Laravel的命名规范,CRUD将根据传递给php artisan blueprint:make命令的CRUD_NAME参数生成资源名称。
示例
{
"crud": {
"name": "Post",
"namespace": "Posts",
"isApi": true
},
"controller": {
"name": "PostsController",
"pagination": 10,
"validations": []
},
"model": {
"name": "Post",
"fillable": "",
"hidden": "",
"softDeletes": false,
"relationships": []
},
"table": {
"name": "posts",
"schema": {
"fields": [],
"keys": {
"primary": "id",
"foreign": [],
"indexes": []
},
"softDeletes": false
}
},
"route": {
"name": "posts",
"url": "posts",
"middlewares": []
}
}
生成CRUD
为了生成我们创建的CRUD,我们使用以下命令
php artisan blueprint:generate CRUD_NAME
示例
php artisan blueprint:generate Post
注意:注意我们使用的是CRUD名称而不是CRUD文件名称,此命令将尝试在您的
database/blueprints文件夹下查找提供的名称的CRUD蓝图,如果没有找到,它将提示您创建该名称的新CRUD。
运行此命令后,将生成以下文件
- 控制器
- 模型
- 请求
- 响应
- 迁移
- 测试
默认情况下,生成器将尝试将CRUD路由添加到您的Route文件中。以下是一个示例:Route::apiResource('route-name', 'controller-name');
运行CRUD迁移
在生成CRUD并创建所有资源后,运行迁移命令
php artisan migrate
CRUD蓝图示例
{
"crud": {
"name": "Post",
"namespace": "Content",
"isApi": true
},
"controller": {
"name": "PostsController",
"namespace": "Content",
"pagination": 10,
"validations": [
{
"field": "title",
"rules": "required|min:5|unique:posts"
},
{
"field": "content",
"rules": "required|min:5"
}
]
},
"model": {
"name": "Post",
"namespace": "Content",
"fillable": "title,content",
"hidden": "user_id",
"softDeletes": true,
"relationships": [
{
"name": "user",
"type": "belongsTo",
"class": "App\\User"
}
]
},
"table": {
"name": "posts",
"schema": {
"fields": [
{
"name": "title",
"type": "string"
},
{
"name": "content",
"type": "text"
}
],
"keys": {
"primary": "id",
"foreign": [
{
"column": "user_id",
"references": "id",
"on": "users",
"onDelete": "cascade",
"onUpdate": "cascade"
}
],
"indexes": [
{
"field": "title",
"type": "unique"
},
{
"field": "title",
"type": "index"
}
]
},
"softDeletes": true
}
},
"route": {
"name": "posts",
"namespace": "Posts",
"url": "posts",
"middlewares": []
}
}
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅contributing.md。
安全
如果您发现任何安全相关的问题,请通过电子邮件联系作者而不是使用问题跟踪器。
鸣谢
许可
许可。有关更多信息,请参阅许可文件。