博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
larabel Artisan Command 使用总结
阅读量:4594 次
发布时间:2019-06-09

本文共 2513 字,大约阅读时间需要 8 分钟。

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);}

enter image description here

代码调用命令
  • 路由方式调用
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'    ]);}

转载于:https://www.cnblogs.com/frankltf/p/10174421.html

你可能感兴趣的文章
【WEB前端经验之谈】时间一年半,或沉淀、或从零开始。
查看>>
优云软件助阵GOPS·2017全球运维大会北京站
查看>>
linux 装mysql的方法和步骤
查看>>
poj3667(线段树区间合并&区间查询)
查看>>
51nod1241(连续上升子序列)
查看>>
SqlSerch 查找不到数据
查看>>
集合相关概念
查看>>
Memcache 统计分析!
查看>>
(Python第四天)字符串
查看>>
个人介绍
查看>>
使用python动态特性时,让pycharm自动补全
查看>>
关于R软件的安装
查看>>
MySQL数据库免安装版配置
查看>>
你必知必会的SQL面试题
查看>>
html5 Canvas绘制时钟以及绘制运动的圆
查看>>
Unity3D热更新之LuaFramework篇[05]--Lua脚本调用c#以及如何在Lua中使用Dotween
查看>>
JavaScript空判断
查看>>
洛谷 P1439 【模板】最长公共子序列(DP,LIS?)
查看>>
python timeit
查看>>
Wireless Network 并查集
查看>>