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 使用总结的更多相关文章

  1. laravel 5.0 artisan 命令列表(中文简体)

    #php artisan list Laravel Framework version Usage: [options] command [arguments] Options(选项): --help ...

  2. laravel artisan 命令工具

    //全局相关 php artisan:显示详细的命令行帮助信息,同 php artisan list php artisan –help:显示帮助命令的使用格式,同 php artisan help ...

  3. Lavarel artisan 命令

    [alex@iZ25c5aeyiiZ yiqizou3.0]# php artisan list Laravel Framework version Usage: command [options] ...

  4. laravel command命令行

    生成类 为了创建一个新命令,你可以使用Artisan中的 command:make 命令生成一个骨架作为你的起点: 生成一个命令类 php artisan command:make FooComman ...

  5. console command

    routes/console.php copy一个默认的 Artisan::command('hello', function () { $this->comment('hello world' ...

  6. 【Laravel】 安装及常用的artisan命令

    composer Laravel 安装 cmd composer create-project laravel/laravel Laravel5 之后自动创建 常用的artisan命令 全局篇 查看a ...

  7. lavavel 定时任务 (command的第二个参数)

    之前好像没有写过,记录一下 $schedule->command()方法 第一个参数不用说,可以传纯字符串或者类::class,不过第二个参数确很少人提到 /** * Add a new Art ...

  8. (1) laravel php artisan list make

    php artisan list make Laravel Framework 5.4.36 Usage: command [options] [arguments] Options: -h, --h ...

  9. Linux 定时任务执行 php artisan

    */ * * * * php /www/wwwroot/project/artisan command:exec postNews 5分钟执行一次

随机推荐

  1. 一点资讯 视频抓取 phantomjs

    # _*_ coding: utf- _*_ """ 思路: .列表页使用phantomjs模拟点击.每个链接只抓取第一页9-10条内容,按照标题去重 .布置定时任务,每 ...

  2. JNDI数据源的使用

    有时候我们数据库的连接会使用jndi的方式 try { InitialContext ic = new InitialContext(); dataSource = (DataSource) ic.l ...

  3. DEVOPS技术实践_20:串联多个job执行

    在jenkins可能会有战役中场景,就是在一个job执行完之后,把这个执行结果作为另一个job的执行条件 比如A执行完,如果A执行成功,则执行B,如果失败则执行C 1 前期准备 A任务 import ...

  4. CentOS 下 maven 安装

    获取maven安装包 wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bi ...

  5. ORM之炀,打造自已独特的开发框架CRL

    ORM一直是长久不衰的话题,各种重复造轮子的过程一直在进行,轮子都一样是圆的,你的又有什么特点呢? CRL这个轮子造了好多年,功能也越来越标准完备,在开发过程中,解决了很多问题,先上一张脑图描述CRL ...

  6. Theia——云端和桌面版的IDE

    Theia是一个利用最新的web技术开发的支持云端和桌面运行的类似IDE的产品,它是一个可扩展的平台,并且全面支持多语言. 目标 建立一个可搭建类似IDE产品的平台 为终端用户提供完整的多语言IDE( ...

  7. linux MySQL 5.7+keepalived 主备服务器自主切换

    一.环境准备1.关闭防火墙与selinux systemctl stop firewalld setenforce 0 sed -i 's/SELINUX=.*/SELINUX=disabled/g' ...

  8. Codeforces Round #524 (Div. 2)(前三题题解)

    这场比赛手速场+数学场,像我这样读题都读不大懂的蒟蒻表示呵呵呵. 第四题搞了半天,大概想出来了,但来不及(中途家里网炸了)查错,于是我交了两次丢了100分.幸亏这次没有掉rating. 比赛传送门:h ...

  9. 1073 多选题常见计分法 (20分)C语言

    批改多选题是比较麻烦的事情,有很多不同的计分方法.有一种最常见的计分方法是:如果考生选择了部分正确选项,并且没有选择任何错误选项,则得到 50% 分数:如果考生选择了任何一个错误的选项,则不能得分.本 ...

  10. Spring Boot2 系列教程 (十八) | 整合 MongoDB

    微信公众号:一个优秀的废人.如有问题,请后台留言,反正我也不会听. 前言 如题,今天介绍下 SpringBoot 是如何整合 MongoDB 的. MongoDB 简介 MongoDB 是由 C++ ...