内容へ移動
fl8 Wiki
ユーザ用ツール
ログイン
サイト用ツール
検索
ツール
文書の表示
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
ログイン
>
最近の変更
メディアマネージャー
サイトマップ
現在位置:
Dokuwiki.fl8.jp
»
11_php
»
02_framework
»
01_laravel
»
16 Laravel コマンドラインアプリケーション
トレース:
11_php:02_framework:01_laravel:16_laravel_command_line
この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。
====== 16 Laravel コマンドラインアプリケーション ====== ===== Commandクラス作成 ===== <code> $ php artisan make:command sample/test $ ll app/Console/Commands/sample/test.php -rw-r--r-- 1 matsui users 677 Jan 30 16:01 app/Console/Commands/sample/test.php </code> ===== Commandのシグネチャ変更 ===== <code|app/Console/Commands/sample/test.php > protected $signature = 'command:name'; ↓ protected $signature = 'command:test'; protected $description = 'Command description'; ↓ protected $description = 'てすとコマンド'; </code> ===== Commandクラスをartisanコマンドとして登録 ===== <code|app/Console/Kernel.php> protected $commands = [ // ]; ↓ protected $commands = [ Commands\sample\test::class ]; </code> ===== Command確認 ===== <code> $ php artisan ・ ・ ・ command command:test てすと ・ ・ ・ </code> ===== 引数を指定する場合 ===== 必須引数 protected $signature = 'command:test {arg}'; 任意引数 protected $signature = 'command:test {arg?}'; デフォルト値の指定 protected $signature = 'command:test {arg=argument}'; 引数に説明を付ける場合 protected $signature = 'command:test {arg : 引数を指定}'; 例: <code> $ php artisan command:test -h Description: てすとコマンド Usage: command:test <arg> Arguments: arg 引数を指定 </code> ==== 引数実行時の取得 ==== <code> public function handle() { $name = $this->argument("name"); $this->info("Hello $name"); } </code> ===== オプションを指定する場合 ===== オプション指定 protected $signature = 'command:test {--dry-run}'; 値指定オプション protected $signature = 'command:test {--dry-run=}'; デフォルト値オプション protected $signature = 'command:test {--dry-run=1}'; オプションに説明を追加 protected $signature = 'command:test {--dry-run : 実行テスト}'; オプションにショートカット指定 protected $signature = 'command:test {--d|--dry-run : 実行テスト}'; 例: <code> $ php artisan command:test -h Description: てすとコマンド Usage: command:test [options] Options: -d, --dry-run 本番実行 </code> ==== オプション実行時の取得 ==== <code> public function handle() { $dry_run = $this->option("dry-run"); $this->info("Option $dry_run"); } </code> ===== コンソール出力 ===== <code> public function handle() { $this->info('info'); $this->line('line'); $this->comment('comment'); $this->question('question'); $this->error('error'); $this->table( ['名前', '年齢'], [ ['Taro', 10], ['Laravel', 5], ] ); } </code> ===== artisanコマンドからartisanコマンドを呼び出す方法 ===== === $this->call === <code> $this->call('command:called', ['--path' => 'call']); </code> === Artisan::call === <code> use \Artisan; // 実行 Artisan::call('command:called', ['--path' => 'artisan']); </code> {{tag>laravel php}}
11_php/02_framework/01_laravel/16_laravel_command_line.txt
· 最終更新: 2019/12/10 01:22 by
matsui
ページ用ツール
文書の表示
以前のリビジョン
バックリンク
文書の先頭へ