ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] # 命令行 ## 简介 ``` // 查看所有可用的 Artisan 命令的列表 $ php artisan list // 查看命的帮助界面 ,显示可用参数及选项 $ php artisan help migrate ``` ### Tinker 命令 (REPL) ``` // 运行 Artisan 命令 tinker 进入 Tinker 环境 $ php artisan tinker // 使用 vendor:publish 命令发布 Tinker 配置文件 $ php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider" ``` ### 命令白名单 >[success]Tinker 通过采用白名单的方式来确定允许哪些 Artisan 命令可以在 shell 中运行。默认情况下,你可以运行 `clear-compiled` 、`down`、`env`、 `inspire`、`migrate`、 `optimize`、和 `up` 命令。 ``` // 在 config/tinker.php 配置文件中的 commands 数组设置 'commands' => [ // App\Console\Commands\ExampleCommand::class, ], ``` ### 别名黑名单 // 在 config/tinker.php 配置文件中的 dont_alias 数组设置 'dont_alias' => [ App\User::class, ], ## 编写命令 ### 生成命令 ``` $ php artisan make:command SendEmails ``` ### 命令结构 ``` // signature 和 description 描述命令与作用 <?php namespace App\Console\Commands; use App\User; use App\DripEmailer; use Illuminate\Console\Command; class SendEmails extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'email:send {user}'; /** * The console command description. * * @var string */ protected $description = 'Send drip e-mails to a user'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @param \App\DripEmailer $drip * @return mixed */ public function handle(DripEmailer $drip) { $drip->send(User::find($this->argument('user'))); } } ``` ### 闭包命令 ``` // routes/console.php Artisan::command('build {project}', function ($project) { $this->info("Building {$project}!"); }); // 输出 Building test! $ php artisan build test ``` ### 类型提示依赖 ``` use App\User; use App\DripEmailer; Artisan::command('email:send {user}', function (DripEmailer $drip, $user) { $drip->send(User::find($user)); }); ``` ### 闭包命令描述 ``` // 使用 describe 方法来为闭包命令添加描述 Artisan::command('build {project}', function ($project) { $this->info("Building {$project}!"); })->describe('Build the project'); ``` ## 定义输出期望 ``` // 参数 // 命令定义了一个必须的参数: user protected $signature = 'email:send {user}'; // 可选参数... email:send {user?} // 带有默认值的可选参数... email:send {user=foo} // 选项 // 不接收值的选项,如果 --queue 存在,该选项的值为 true, 否则为 false protected $signature = 'email:send {user} {--queue}'; $ php artisan email:send 1 --queue // 带值的选项 protected $signature = 'email:send {user} {--queue=}'; $ php artisan email:send 1 --queue=default // 选项简写 email:send {user} {--Q|queue} // 参数输入数组 email:send {user*} // 设置 user 的值为 ['foo', 'bar'] $ php artisan email:send foo bar // 选项输入数组 email:send {user} {--id=*} $ php artisan email:send --id=1 --id=2 // 输入说明 protected $signature = 'email:send {user : The ID of the user} {--queue= : Whether the job should be queued}'; ``` ## Command I/O ### 获取输入 ``` // 如果参数或选项不存在,则返回 null 。 // 用 argument 和 option 方法接收参数和选项值 public function handle() { $userId = $this->argument('user'); } // 获取所有参数的数组 $arguments = $this->arguments(); // 获取一个指定的选项值 $queueName = $this->option('queue'); // 获取所有的选项值 $options = $this->options(); ``` ### 交互式输入 ``` public function handle() { $name = $this->ask('What is your name?'); } // secret 方法输入内容是不可见的,用于敏感信息的输入 $password = $this->secret('What is the password?'); ``` ### 请求确认 ``` // 默认返回 false,如果用户输入 y 或者 yes 则返回 true if ($this->confirm('Do you wish to continue?')) { // } ``` ### 自动补全 ``` // 可以选择,或者任意回答 $name = $this->anticipate('What is your name?', ['Taylor', 'Dayle']); ``` ### 多重选择 ``` // 预设的选择,可以设置默认值的索引 $name = $this->choice('What is your name?', ['Taylor', 'Dayle'], $defaultIndex); ``` ### 编写输出 >[success] 可以使用 `line` 、 `info` 、 `comment` 、 `question` 和 `error` 方法来将输出发送到终端。每个方法都使用适当的 `ANSI` 颜色表明其目的。 ``` // 绿色 public function handle() { $this->info('Display this on the screen'); } // 红色 $this->error('Something went wrong!'); // 无颜色 $this->line('Display this on the screen'); ``` ### 表格布局 ``` $headers = ['Name', 'Email']; $users = App\User::all(['name', 'email'])->toArray(); $this->table($headers, $users); ``` ### 进度条 >[success] 对于耗时任务,提示进度非常有必要。使用 output 对象就可以创建、加载以及停止进度条。首先,定义好任务总步数,然后,在每次任务执行时加载进度条。 ``` $users = App\User::all(); $bar = $this->output->createProgressBar(count($users)); $bar->start(); foreach ($users as $user) { $this->performTask($user); $bar->advance(); } $bar->finish(); ``` ## 注册命令 >[info] `app/Console/Commands` 目录下的命令都会被注册,这是由于控制台内核的 `commands` 方法调用了 `load`。实际上,可随意调用 `load` 来扫描其他目录下的 Artisan 命令,也可以在 `app/Console/Kernel.php` 文件的 `$commands` 属性中手动注册命令的类名。Artisan 启动时,这个属性列出的命令都将由 [服务容器](/docs/{{version}}/container) 解析并通过 Artisan 进行注册。 ``` protected function commands() { $this->load(__DIR__.'/Commands'); $this->load(__DIR__.'/MoreCommands'); } protected $commands = [ Commands\SendEmails::class ]; ``` ## 程序调用命令 ``` use Illuminate\Support\Facades\Artisan; Route::get('/foo', function () { $exitCode = Artisan::call('email:send', [ 'user' => 1, '--queue' => 'default' ]); // }); // 命令字符串 Artisan::call('email:send 1 --queue=default'); // 队列化 Route::get('/foo', function () { Artisan::queue('email:send', [ 'user' => 1, '--queue' => 'default' ]); // }); // 队列化派发的连接或任务 Artisan::queue('email:send', [ 'user' => 1, '--queue' => 'default' ])->onConnection('redis')->onQueue('commands'); ``` ### 传递数组值 ``` // 数组的选项 Route::get('/foo', function () { $exitCode = Artisan::call('email:send', [ 'user' => 1, '--id' => [5, 13] ]); }); ``` ### 传递布尔值 ``` $exitCode = Artisan::call('migrate:refresh', [ '--force' => true, ]); ``` ### 命令的互相调用 ``` // 在命令内再调用命令 public function handle() { $this->call('email:send', [ 'user' => 1, '--queue' => 'default' ]); // } // 控制台不显示输出 $this->callSilent('email:send', [ 'user' => 1, '--queue' => 'default' ]); ```