jsiebach / commander
由Laravel Backpack驱动的执行Artisan命令的界面
Requires
- php: >=7.0.0
- backpack/base: 1.0.*
- backpack/crud: 3.5.*
This package is not auto-updated.
Last update: 2024-09-15 04:26:13 UTC
README
Backpack Commander是一个简单的CRUD界面,用于在Backpack for Laravel中运行Artisan命令。主要优点是快速允许非开发人员运行Artisan命令,而无需访问命令行,并且可以轻松使用Backpack字段定义配置命令行参数。
安装
-
安装Backpack Commander
composer require jsiebach/commander
- 发布配置文件
php artisan vendor:publish --provider="JSiebach\Commander\CommanderServiceProvider" --tag="config"
- 发布迁移
php artisan vendor:publish --provider="JSiebach\Commander\CommanderServiceProvider" --tag="migrations"
- 审查配置文件以更新您的应用程序配置。可用选项包括
route
:编辑commander界面的路由。默认为/admin/commander/command
。
allow_creation_and_deletion
:确定是否可以添加或删除新命令。建议在生产环境中保持此选项为false
,并直接在数据库中添加新命令。
-
运行
php artisan migrate
-
将您的命令界面链接添加到侧边栏(可选)
<li><a href="{{ backpack_url('commander')."/command" }}"><i class="fa fa-bullhorn"></i> <span>命令</span></a></li>
向Commander添加命令
您现在可以向您的commander_commands
表添加新命令。字段包括
command
:Artisan命令(例如,inspire
- 您将为php artisan XXXXX
填写的任何内容)
descriptive_name
:用于显示给用户的命令名称。将默认为Artisan命令名称
CRUD界面还将显示一个包含命令的description
属性的列。
定义命令
要定义命令,请向命令添加一个名为getCommanderFields()
的函数,该函数应返回一个Backpack字段定义数组。
-
对于像
inspire
这样的简单命令,您可以完全省略getCommanderFields()
函数。 -
对于需要参数和选项的命令,您应在命令的
getCommanderFields()
函数中返回Backpack字段配置。例如
<?php
namespace App\Console\Commands;
use App\User;
use Illuminate\Console\Command;
use JSiebach\Commander\CommandableInterface;
class RunNewUsersReport extends Command implements CommandableInterface
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'reports:run-new-users-report {--startdate=} {--include-admins} {--delivery-email=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Run a report listing the newest users';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// You can use the arguments of the artisan command as normal:
$this->comment('The email address to send the report to is '.$this->option('email'));
$this->comment('The option \'include-admins\' is '.(((boolean) $this->option('include-admins')) ? "true" : "false"));
$this->comment('The start date for the report is '.$this->option('start-date'));
}
/**
* @return array
*/
public function getCommanderFields() {
return [
[
'name' => '--startdate',
'type' => 'date_picker',
'label' => 'Start Date'
],
[
'name' => '--include-admins',
'type' => 'checkbox',
'label' => 'Include Admin Users'
],
[
'name' => '--delivery-email',
'type' => 'select2_from_array',
'options' => User::all()->pluck('name', 'email'),
'label' => 'Report Recipient'
]
];
}
}
注意
- 您不能使用关系字段,因为没有要关联的模型。请坚持使用返回字符串、布尔值或数组的简单字段类型。
运行命令
在命令CRUD界面中,单击运行
按钮以运行命令。您将看到由命令上定义的字段生成的表单。
填写字段并单击运行
。命令的输出将打印在结果视图中。
排队命令
如果您想排队运行命令,请添加字段--queue_command
。如果您想将其作为选项,可以使用复选框。如果要将它设置为自动,请使用具有值1的隐藏字段。
您可以使用--queue_name
选项指定队列名称。默认为default
。
未来的开发计划
- 处理命令错误
- 考虑在没有表格的情况下生成索引视图的方法(即在应该包含的命令上定义
$commandable = true
)
支持
请在GitHub上提交问题或拉取请求。
许可证
MIT