larabel Artisan Command 使用总结
larabel Artisan Command 使用总结
定义命令
- 在routes/console.php下定义命令
Artisan::command('ltf', function () {
(new \App\Services\EditService())->edit();
$this->comment("news sent");
})->describe('Send news');
//调用
> php artisan ltf
- 通过artisan make:command来自动生成(以SendEmails为例)
- php artisan make:command SendEmails 会在app/Console/Commands下创建SendEmails.php 文件
- 编写SendEmails 类和调用
use Illuminate\Console\Command;
use Redis;
class SendEmails extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
//这里必须要填 格式是[命令名] [name参数] [选项参数]
//调用示例 php artisan ltf:ltftest aaa --que
protected $signature = 'ltf:ltftest {name}{--que}';
/**
* The console command description.
*
* @var string
*/
//这里是命令描述
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
// handle 方法在命令执行时被调用,将所有命令逻辑都放在这个方法里面。
public function handle()
{
Redis::set('ttttt','dass');
}
}
命令参数获取
- 获取参数
$this->argument('name'); //返回某个参数的值
$this->arguments(); //返回所有参数,数组格式
- 获取选项参数
// 获取指定选项...
$queueName = $this->option('queue');
// 获取所有选项...
$options = $this->options();
命令行交互
- 命令执行期间要用户提供输入
public function handle(){
$name = $this->ask('What is your name?');
}
- 命令执行期间要用户提供输入敏感信息
public function handle(){
$name = $this->secret('What is your password?');
}
- 让用户确认
public function handle(){
$this->confirm('Do you wish to continue? [y|N]')
}
- 给用户提供选择
public function handle(){
$name = $this->choice('What is your name?', ['Taylor', 'Dayle']);
}
- 编写输出,将输出发送到控制台
public function handle(){
$this->info('Display this on the screen');
$this->error('Display this on the screen');
$this->line('Display this on the screen');
}
- 表格布局
public function handle(){
$headers = ['Name', 'Email'];
$users = App\User::all(['name', 'email'])->toArray();
$this->table($headers, $users);
}

代码调用命令
- 路由方式调用
Route::get('/foo', function () {
$exitCode = Artisan::call('email:send', [
'user' => 1, '--queue' => 'default'
]);
});
- queue调用
Route::get('/foo', function () {
Artisan::queue('email:send', [
'user' => 1, '--queue' => 'default'
]);
});
- 在一个命令中调用其它命令
/**
* 执行控制台命令
*
* @return mixed
*/
public function handle(){
$this->call('email:send', [
'user' => 1, '--queue' => 'default'
]);
}
larabel Artisan Command 使用总结的更多相关文章
- laravel 5.0 artisan 命令列表(中文简体)
#php artisan list Laravel Framework version Usage: [options] command [arguments] Options(选项): --help ...
- laravel artisan 命令工具
//全局相关 php artisan:显示详细的命令行帮助信息,同 php artisan list php artisan –help:显示帮助命令的使用格式,同 php artisan help ...
- Lavarel artisan 命令
[alex@iZ25c5aeyiiZ yiqizou3.0]# php artisan list Laravel Framework version Usage: command [options] ...
- laravel command命令行
生成类 为了创建一个新命令,你可以使用Artisan中的 command:make 命令生成一个骨架作为你的起点: 生成一个命令类 php artisan command:make FooComman ...
- console command
routes/console.php copy一个默认的 Artisan::command('hello', function () { $this->comment('hello world' ...
- 【Laravel】 安装及常用的artisan命令
composer Laravel 安装 cmd composer create-project laravel/laravel Laravel5 之后自动创建 常用的artisan命令 全局篇 查看a ...
- lavavel 定时任务 (command的第二个参数)
之前好像没有写过,记录一下 $schedule->command()方法 第一个参数不用说,可以传纯字符串或者类::class,不过第二个参数确很少人提到 /** * Add a new Art ...
- (1) laravel php artisan list make
php artisan list make Laravel Framework 5.4.36 Usage: command [options] [arguments] Options: -h, --h ...
- Linux 定时任务执行 php artisan
*/ * * * * php /www/wwwroot/project/artisan command:exec postNews 5分钟执行一次
随机推荐
- JDK、JRE、JVM、Android SDK、Android Studio
===================================================== 参考链接: JDK\JRE\JVM:https://www.cnblogs.com/bola ...
- Visual Studio 2019 编译.Net Core Console项目出现【MSB4018 The "CreateAppHost" task failed unexpectedly】 错误
需要测试一个小东东,使用Visual Studio 2019新建了一个.Net Core的Console程序,但是在编译的时候一直报错,死活编译不通过. 错误信息: Severity Code Des ...
- The sixth day of Crawler learning
爬取我爱竞赛网的大量数据 首先获取每一种比赛信息的分类链接 def get_type_url(url): web_data = requests.get(web_url) soup = B ...
- 将Samba设置为Active Directory域控制器
一 简介 从版本4.0开始,samba可以作为Active Directory(AD)域控制器(DC)运行,如果在生产环境中安装samba,建议运行两个或者多个DC用于故障转移 本文介绍如何让将一个S ...
- jib-maven-plugin构建镜像
序言 在本次期末设计当中,应为需要做部署脚本,我们采用的是dockerfile+docker-compose的部署方式,这种方式对vue项目是没有问题的,因为vue下载依赖与打包是分离开来的,即使修改 ...
- Java实现上传文件到指定服务器指定目录
前言需求 使用freemarker生成的静态文件,统一存储在某个服务器上.本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch.毕竟有现成的就很舒服,在此介绍给大家. 具体实现 引入的pom ...
- 解决 Table ‘performance_schema.session_variables’ doesn’t exist 问题
performance_schema在mysql5.5以上就有自带 performance_schema(安装数据库时自带的)如果装数据库或者使用数据时不小心删除了,就会出现Table‘perform ...
- 25.xlrd、xlwt和openpyxl模块的比较和使用
xlrd.xlwt和openpyxl模块的比较:1)xlrd:对xls.xlsx.xlsm文件进行读操作–读操作效率较高,推荐2)xlwt:对xls文件进行写操作–写操作效率较高,但是不能执行xlsx ...
- 你的java服务挂了吗
问题背景 最近测试环境服务总是崩溃,运维小哥全部重启后还是崩溃,查看了服务运行情况占用内存确实挺高的,看来是时候优化一波jvm参数了. 优化前 top $(ps -e | grep java | aw ...
- 应届生/社招面试最爱问的几道Java基础问题
本文已经收录自笔者开源的 JavaGuide: https://github.com/Snailclimb ([Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核心知识)如果觉得不错 ...