larabel Artisan Command 使用总结
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 使用总结的更多相关文章
- laravel 5.0 artisan 命令列表(中文简体)
#php artisan list Laravel Framework version Usage: [options] command [arguments] Options(选项): --help ...
- laravel artisan 命令工具
//全局相关 php artisan:显示详细的命令行帮助信息,同 php artisan list php artisan –help:显示帮助命令的使用格式,同 php artisan help ...
- Lavarel artisan 命令
[alex@iZ25c5aeyiiZ yiqizou3.0]# php artisan list Laravel Framework version Usage: command [options] ...
- laravel command命令行
生成类 为了创建一个新命令,你可以使用Artisan中的 command:make 命令生成一个骨架作为你的起点: 生成一个命令类 php artisan command:make FooComman ...
- console command
routes/console.php copy一个默认的 Artisan::command('hello', function () { $this->comment('hello world' ...
- 【Laravel】 安装及常用的artisan命令
composer Laravel 安装 cmd composer create-project laravel/laravel Laravel5 之后自动创建 常用的artisan命令 全局篇 查看a ...
- lavavel 定时任务 (command的第二个参数)
之前好像没有写过,记录一下 $schedule->command()方法 第一个参数不用说,可以传纯字符串或者类::class,不过第二个参数确很少人提到 /** * Add a new Art ...
- (1) laravel php artisan list make
php artisan list make Laravel Framework 5.4.36 Usage: command [options] [arguments] Options: -h, --h ...
- Linux 定时任务执行 php artisan
*/ * * * * php /www/wwwroot/project/artisan command:exec postNews 5分钟执行一次
随机推荐
- Unitils集成DBUnit、Spring-单元测试(转)
1.maven-pom文件中引入相关jar包 <!-- Unitils -dbunit.Spring --> <dependency> <groupId>org.u ...
- 优雅的使用BeanUtils对List集合的操作
摘要 在业务员流程的时候,我们在Entity.Bo.Vo层数据间可能经常转换数据,Entity对应的是持久层数据结构(一般是数据库表的映射模型).Bo对应的是业务层操作的数据结构.Vo就是Contro ...
- Ecshop在模板中判断用户是否登陆,获取用户等级信息
ecshop模板中smarty怎样判断用户等级.用户id.用户昵称用户名,请看以下方法,使用全局变量 <!-- {if $smarty.session.user_rank gt 1}--> ...
- Word List 1 part 1
inter- 在....之间 intermediate adj. 中间的 pro- 向前,在前;很多;赞同;亲... proportion n. 比例;部分 prim- 第一,主要的 prime ad ...
- Win7旗舰版仅供测试支持正版
系统效果展示 安装后唯一标准的桌面截图:(如发现安装后与本图不一致,均为第三方安装工具捆绑所为,请注意使用工具!慎用XX桃.XX菜.uXX之类的工具,建议使用推荐的方法安装) 如此清新简洁的安装界面, ...
- 再也不怕和老外交流了!我用python实现一个微信聊天翻译助手!
前言 在前面的一篇文章如何用python“优雅的”调用有道翻译中咱们清楚的写过如何一层一层的解开有道翻译的面纱,并且笔者说过那只是脑洞的开始.现在笔者又回来了.当你遇到一些外国小哥哥小姐姐很心动.想结 ...
- ENS 域名注册表智能合约(ENSRegistry.sol)解析
ENS 注册表合约是 ENS 系统中的核心合约,了解这个合约可以敲开我们理解 ENS 域名系统的大门. 打开下面的折叠区域可以查看用 Solidity 语言编写的详细代码.当前部署在以太坊中的 ENS ...
- C#多线程与异步
1.什么是异步同步 如果一个方法被调用,调用者需要等待该方法被执行完毕之后才能继续执行,则是同步. 如果方法被调用后立刻返回,即使该方法是一个耗时操作,也能立刻返回到调用者,调用者不需要等待该方法,则 ...
- C#实现DataTable转.CSV文件
将DataTable转换成CSV文件是一种常见的转换形式,主要通过遍历Table的每行,再对每行遍历每列,实现对数据的读取,然后用分隔符分隔Table的每个栏位数据,把读取的字符写入到CSV文件中.这 ...
- Java ArrayList类的简单介绍
ArrayList类的说明: ArrayList类是List接口的实现类,java.util.ArrayList集合数据存储的结构是数组结构. 特点: 元素增删慢,查找快.(由于日常开发中使用最多的功 ...