abdelilahlbardi / laragenerator
在构建 Laravel 项目时避免重复代码。
Requires
- laracasts/generators: master@dev
This package is not auto-updated.
Last update: 2021-02-05 23:53:52 UTC
README
Laravel 5.4 生成器
构建 Laravel 项目始于一个想法。那么为什么不构建主要想法的行为,而将其余部分留给 Laragenerator 来引导你的项目呢?此软件包帮助你更多地关注你的项目想法。 避免重复代码。
当前功能帮助你
- Create Namespaces.
- Create Controllers.
- Create Form Requests.
- Create Models.
- Create Models Relationships.
- Create Migrations.
- Create Routes Files.
- Create Views files.
如果你有任何建议,请告诉我: https://github.com/AbdelilahLbardi/Laragenerator/pulls。
安装
首先,通过 Composer 引入软件包。
"require": {
"laracasts/generators": "master@dev",
"abdelilahlbardi/laragenerator": "^1.5"
}
然后运行
composer update
之后,在 config/app.php
中包含服务提供者。
'providers' => [
AbdelilahLbardi\LaraGenerator\Providers\LaraGeneratorServiceProvider::class,
];
使用
此软件包扩展 Laravel Artisan 并添加 generate:resource
命令
以下是一个快速示例,说明如何启动你的 Laravel 想法
php artisan generate:resource Backend/Article "titles:string, content:text"
名称 | 类型 | 示例 | 用法 |
模型 | 参数 | 后端/文章 | 必需 |
模式 | 参数 | --schema="title:string, content:text, slug:string:unique" | 可选 |
关系 | 选项 | --relations="hasmany:tags, belongsto:user" | 可选 |
无控制器 | 选项 | --without-controller | 可选 |
无模型 | 选项 | --without-model | 可选 |
无请求 | 选项 | --without-request | 可选 |
无视图 | 选项 | --without-views | 可选 |
无路由 | 选项 | --without-routes | 可选 |
无迁移 | 选项 | --without-migration | 可选 |
使用闪存 | 选项 | --with-flash | 可选 |
回滚 | 选项 | --rollback | 可选 |
示例
使用所有资源启动
php artisan generate:resource Article "title:string, content:text, slug:string:unique, user_id:integer:foreign"
你将注意到在你的项目中出现额外的文件和文件夹
Controllers/ArticlesController.php
: 这里是你的生成控制器,位于你指定的命名空间中。app/Models/Article
: 新的 Models 文件夹。resources/views/articles/index.blade.php
: 空的 index 视图。resources/views/articles/create.blade.php
: 空的 create 视图。resources/views/articles/edit.blade.php
: 空的 edit 视图。routes/web/articles.php
: 已生成的路由。database/migrations/yyyy-mm-dd-create_articles_table.php
: 这里是你的迁移。
无模型启动
php artisan generate:resource Backed/Item "title:string, price:float, slug:string:unique, category_id:integer:foreign" --without-model
这将生成与最近命令相同的文件,但不会包括
Controllers/Backend/ItemsController.php
: 这里是你的生成控制器,位于你指定的命名空间中。resources/views/backend/items/index.blade.php
: 空的 index 视图。resources/views/backend/items/create.blade.php
: 空的 create 视图。resources/views/backend/items/edit.blade.php
: 空的 edit 视图。routes/web/backend/items.php
: 已生成的路由。database/migrations/yyyy-mm-dd-create_items_table.php
: 这里是你的迁移。
使用空迁移进行引导
由于--schema
是一个可选选项,如果你没有指定--schema
选项,Laragenerator将生成一个包含简单id和时间的空模型。
要这样做,这里有一个简单的命令。
php artisan generate:resource Backed/Item
迁移文件将看起来像
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateItemsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('items', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('items'); } }
回滚
php artisan generate:resource Frontend/User --rollback
使用Laragenerator的`--rollback`选项,你不需要手动删除生成的文件夹。它为你完成所有工作。
请注意,如果你有更多控制器,此选项不会删除命名空间中的所有控制器。
模板发布
你可能想自定义模板,如下 artisan 命令所示
php artisan vendor:publish --tag=templates
这将向你的应用添加一个名为Tempaltes
的新文件夹,其中包含以下文件
Templates/Controller/Controller.txt
: 控制器模板。Templates/Model/Model.txt
: 模型模板。Templates/View/index.txt
: 索引视图模板。Templates/View/headerCell.txt
: HTML表格元素标题行模板。Templates/View/contentCell.txt
: HTML表格元素内容行模板。Templates/View/create.txt
: 创建视图模板。Templates/View/createInput.txt
: 创建表单输入视图模板。Templates/View/edit.txt
: 编辑视图模板。Templates/View/editInput.txt
: 编辑表单输入视图模板。Templates/routes.txt
: 路由文件模板。
模式
由于Laragenerator使用https://github.com/laracasts/Laravel-5-Generators-Extended来生成迁移和模式
你可以在这里找到相关文档:https://github.com/laracasts/Laravel-5-Generators-Extended。