【转】laravel之Artisan命令操作Artisan Console

本文文档适用于laravel5.2版本,其他版本参考。

artisan 命令查询

php artisan list

artisan 帮助

php artisan help

有没想过自己写一些命令,然后通过artisan执行呢,根据文档,我们来一一操作

创建

php artisan make:console SendEmails
#带命令写入
php artisan make:console SendEmails --command=emails:send

这个时候生成:app\Console\Commands\SendEmails.php文件

解析文件

namespace App\Console\Commands;

use Illuminate\Console\Command;

class SendEmails extends Command
{
    /**
     * The name and signature of the console command.
     *控制台的命令指令,exp:php artisan emails:seed
     * @var string
     */
    protected $signature = 'emails:send';

    /**
     * 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
     */
    public function handle()
    {
       
        
    }
}

传值

#必须传值
protected $signature = 'email:send {user}';
php artisan email:send 1(必须有这个,否则报错)

#可有可无传值
protected $signature = 'email:send {user?}';
php artisan email:send  1(可有可无)

#默认值
protected $signature = 'email:send {user=1024}';
php artisan email:send 1(可有可无)

开关传递

开关:表示一开一关,换成返回未:1/0,true/false

protected $signature = 'email:send {user} {--queue}';
// --queue这个名字可以随便取,避免认为是队列,
//如果--queue开关被传递,其值是true,否则其值是false:

php artisan email:send 1 --queue

开关分配值

//选项值被用户通过=来分配:
protected $signature = 'email:send {user} {--queue=}';
php artisan email:send 1 --queue='on'

选项别名

email:send {user} {--Q|queue}

定义参数和选项以便指定输入数组

这没看懂,不知道在控制台怎么传值

email:send {user*}
email:send {user} {--id=*}

获得传值

argument和option

// 获取指定选项...
$queueName = $this->option('queue');
// 获取所有选项...
$options = $this->option()

输入提示

/**
 * 执行控制台命令
 *
 * @return mixed
 */
public function handle(){
    $name = $this->ask('What is your name?');
   $password = $this->secret('What is the password?');//隐藏
}
//用户确认
if ($this->confirm('Do you wish to continue? [y|N]')) {
    //
}

索引选择

$name = $this->choice('What is your name?', ['Taylor', 'Dayle'], false);
选择,0,1,...

输出,反馈信息

line,info, comment, question 和 error方法

//要显示一条错误消息,使用error方法。错误消息文本通常是红色:
$this->error('Something went wrong!');
//如果你想要显示原生输出,可以使用line方法,该方法输出的字符不带颜色:
$this->line('Display this on the screen');

//表格布局
$headers = ['Name', 'Email'];
$users = App\User::all(['name', 'email'])->toArray();
$this->table($headers, $users);

进度条

$users = App\User::all();

$this->output->progressStart(count($users));

foreach ($users as $user) {
    $this->performTask($user);
    $this->output->progressAdvance();
}

$this->output->progressFinish();

更多查看(http://symfony.com/doc/2.7/components/console/helpers/progressbar.html)

注册命令

app/Console/Kernel.php 把这个文件导入,

protected $commands = [
   'App\Console\Commands\SendEmails'
];`

CLI之外执行Artisan命令

Route::get('/foo', function () {
    $exitCode = Artisan::call('email:send', [
        'user' => 1, '--queue' => 'default'
    ]);
});
//需要指定不接收字符串的选项值
$exitCode = Artisan::call('migrate:refresh', [
    '--force' => true,
]);

使用Artisan上的queue队列

Route::get('/foo', function () {
    Artisan::queue('email:send', [
        'user' => 1, '--queue' => 'default'
    ]);
});

通过其他命令调用命令

已存在的Artisan命令中调用其它命令

/**
 * 执行控制台命令
 *
 * @return mixed
 */
public function handle(){
    $this->call('email:send', [
        'user' => 1, '--queue' => 'default'
    ]);
}

调用其它控制台命令并阻止其所有输出

$this->callSilent('email:send', [
    'user' => 1, '--queue' => 'default'
]);
 
 
2人点赞
 
from:https://www.jianshu.com/p/a880a494d122

posted on 2020-03-13 16:07  神奇的旋风  阅读(1238)  评论(0编辑  收藏  举报

导航