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. Android6_大致了解4大组件

    一.Activity和View Activity是Android应用中负责与用户交互的组件. View是所有UI控件.容器控件的基类.View组件就是Android应用中用户实实在在看到的部分. Ac ...

  2. while Ture怎么退出全部

    while True: #这是一个死循环 如果想要quit出去只能一层一层的退出 比如如果在第三层输入quit会回到第二层 那么有什么方法能够在第三层就退出全部? print("level1 ...

  3. Java泛型类特性

    在2004年末Java推出了Java5,其中提供了对泛型方法和类的支持,也围绕着泛型推出了一下特性,本章将对Java泛型进行综合的概括 1.泛型特性构件pre-Java 5 1.使用Object表示泛 ...

  4. appium+android自动化测试环境部署

    1 node.js安装 官网(https://nodejs.org/en/) 下载对应版本的node.js并安装 安装完成后cmd中输入node -v,输入版本号则安装成功 2 jdk安装 下载对应版 ...

  5. Google Vision OCR

    Google Vision OCR 爬坑建议 首先安装 Google Vision shell composer require google/cloud-vision 第一次使用的时候真的遇到的问题 ...

  6. Docker Swarm Mode简介与核心概念

    什么是Docker Swarm Docker Swarm是Docker官方的一种容器编排方案,用于管理跨主机的Docker容器,可以快速对指定服务进行水平扩展.部署.删除 一个Docker Swarm ...

  7. 洛谷$P2824\ [HEOI2016/TJOI2016]$ 排序 线段树+二分

    正解:线段树+二分 解题报告: 传送门$QwQ$ 昂着题好神噢我$jio$得$QwQQQQQ$,,, 开始看到长得很像之前考试题的亚子,,,然后仔细康康发现不一样昂$kk$,就这里范围是$[1,n]$ ...

  8. Spring Security入门(基于SSM环境配置)

    一.前期准备 配置SSM环境 二.不使用数据库进行权限控制 配置好SSM环境以后,配置SpringSecurity环境 添加security依赖   <dependency> <gr ...

  9. spring boot介绍

    spring boot简介 1.spring boot是spring家族中的一个全新的框架,它用来简化spring应用程序的创建和开发过程,也可以说spring boot能简化我们之前采用ssm框架进 ...

  10. Springboot Jackson配置根本方案, 日期格式化, 时区设置生效

    当项目集成配置的功能越来越多, 说不准哪个配置就影响到了什么. 比如你启用了EnableMvC, 默认配置文件配置的一些文件就失效了. 虽然约定大于配置,让springboot可以极简化构建, 但不熟 ...