meshesha / artisan-make-mvc

Laravel artisan 命令,用于从现有模型创建视图、控制器和路由

dev-main 2023-10-12 20:04 UTC

This package is auto-updated.

Last update: 2024-09-12 22:12:22 UTC


README

Laravel 包,添加 artisan 命令以创建 CRUD 文件和操作,包括:视图(index、create、show、edit)、控制器、添加路由、工厂和测试。它依赖于现有模型。所有这些都在一个命令行中完成。

要求

  • laravel >= 7

安装

下载

通过 composer

composer require meshesha/artisan-make-mvc

发布配置文件

php artisan vendor:publish --provider="Meshesha\ArtisanMakeMvc\ArtisanMakeMvcServiceProvider"

用法

  • 此包依赖于现有模型。因此,需要创建一个模型
php artisan make:model Post -m
  • 在迁移文件中完成所需的列名,并迁移到数据库。

  • 在模型文件中必须添加 $fillable

protected $fillable = [
'title',
'body',
];

命令选项


php artisan make:mvc {model} {--W|incviews=true} {--F|viewfolder=} {--H|includehidden=true} {--C|inccontroller=true} {--R|incroute=true}

model :  the existing model name (required) (e.g. 'Post')
--incviews      | -W : Whether to include\create blade views  (optional) (default value : true).
--viewfolder    | -F : sub folder inside "resources\views" (optional) (default value : db table name of model (e.g. : 'posts'))
--includehidden | -H : Whether to include hidden fields that are mentioned in the model via 'protected $hidden' (optional) (default value : false)
--inccontroller | -C : Whether to include\create controller (optional) (default value : true)
--incroute      | -R : Whether or not to add a route to the routers file (optional) (default value : true)

--factory            : Add factory file (ModelNameFactory.php)  in "database/Factories" directory.
--test               : Add test file (ModelNameTest.php) in "tests/Feature" directory and factory file  in "database/Factories" directory (because it is required for testing).

配置文件(config\ArtisanMakeMvc.php)

return [
    "template" => "default", //templates: default (Vanilla CSS), bootstrap, tailwind
    "extends" => "", //e.g: @extends('layouts.app')
    "section" => "", //e.g: @section('content')
    "endsection" => "", //e.g: @endsection
];

示例

php artisan make:mvc Post

# 'Post' - model name

此命令将创建

并将添加到 routes/web.php 中:对于 laravel 版本 >= 8.0.0

// posts:
Route::resource('posts', App\Http\Controllers\PostController::class);

对于 laravel 版本 < 8.0.0

// posts:
Route::resource('posts', 'PostController');

撤销

撤销最后操作

php artisan mvc:undo

将删除所有最近创建的文件。

更多选项

-L | --list    : shows history list
-S | --select  : select from list for 'undo' action.

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件