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. Python3_函数参数传递、可变与不可变对象、变量作用域、函数返回值

    参数传递: 在 python 中,类型属于对象,变量是没有类型的:(变量都仅仅是一个对象的引用,没有类型之分)a=[1,2,3] a="Runoob" 以上代码中,[1,2,3] ...

  2. Servlet request 面试题

    使用request获得请求行:String getmethod():获得请求的资源:String getcontextpath():----web应用名称request是一个域对象request完成请 ...

  3. Perl 的内置变量$|

    $|是perl的内置变量,默认情况下是0,如果设置为非0的话,表示当前的输出不经过缓存立刻输出.相当于c语言的fflush()函数,立即刷新缓冲区. 比如你print或者write一个文件,实际是需要 ...

  4. 在Spring Boot中使用Docker在测试中进行高级功能测试

    最近又学到了很多新知识,感谢优锐课老师细致地讲解,这篇博客记录下自己所学所想. 想更多地了解Spring Boot项目中的功能测试吗?这篇文章带你了解有关在测试中使用Docker容器的更多信息. 本文 ...

  5. python 批量生成xml标记文件(连通域坐标分割)

    #!/usr/bin/python # -*- coding=utf-8 -*- # author : Manuel # date: 2019-05-15 from xml.etree import ...

  6. http、https、SSL、TLS的区别

    一.HTTP和HTTPS之间的区别 HTTP是一种协议,全称叫作:超文本传输协议(HTTP,HyperText Transfer Protocol),是互联网上应用最为广泛的一种网络协议.所有的WWW ...

  7. alpha week 2/2 Scrum立会报告+燃尽图 06

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/9803 小组名称:“组长”组 组长:杨天宇 组员:魏新,罗杨美慧,王歆瑶, ...

  8. # Okhttp解析—Interceptor详解

    Okhttp解析-Interceptor详解 Interceptor可以说是okhttp的精髓之一,Okhttp重写请求/响应.重试.缓存响应等操作,基本都是在各个Interceptor中完成的,上篇 ...

  9. Java实现上传文件到指定服务器指定目录

    前言需求 使用freemarker生成的静态文件,统一存储在某个服务器上.本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch.毕竟有现成的就很舒服,在此介绍给大家. 具体实现 引入的pom ...

  10. 在64位ubuntu上安装tensorflow

    首先从ubuntu14.04的安装讲起 1.下载ubuntu14.04 64位的系统,下载地址如下: http://www.ubuntu.com/download/desktop 2.下载好64位的u ...