라라벨 아티즌 명령어 생성 artisan make:command

(Artisan make:command에서 넘어옴)

1 개요[ | ]

Artisan Console
Laravel Generating Commands
라라벨 명령어 생성

2 클래스 생성[ | ]

Console
Copy
root@zetawiki:/var/www/laravel# php artisan make:command Greet
Console command created successfully.
root@zetawiki:/var/www/laravel# ll app/Console/Commands/Greet.php
-rw-rw-r-- 1 root root 671 Dec 27 19:18 app/Console/Commands/Greet.php

3 클래스 수정[ | ]

app/Console/Commands/Greet.php
PHP
Copy
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Greet extends Command
{
	protected $signature = 'greet';
	protected $description = 'Greet someone';
	public function __construct()
	{
		parent::__construct();
	}
	public function handle()
	{
		echo "Hello!\n";
		return 0;
	}
}

4 확인[ | ]

Console
Copy
root@zetawiki:/var/www/laravel# php artisan | grep greet
  greet                Greet someone
root@zetawiki:/var/www/laravel# php artisan greet
Hello!
root@zetawiki:/var/www/laravel# php artisan greet John

  Too many arguments, expected arguments "command".

5 (Optional) 클래스 수정 2[ | ]

app/Console/Commands/Greet.php
PHP
Copy
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Greet extends Command
{
	protected $signature = 'greet {who}';
	protected $description = 'Greet someone';
	public function __construct()
	{
		parent::__construct();
	}
	public function handle()
	{
		$who = $this->argument('who');
		echo "Hello, $who!\n";
	}
}
실행예시
Console
Copy
root@zetawiki:/var/www/laravel# php artisan | grep greet
  greet                Greet someone
root@zetawiki:/var/www/laravel# php artisan greet

  Not enough arguments (missing: "who"). 

root@zetawiki:/var/www/laravel# php artisan greet John
Hello, John!
root@zetawiki:/var/www/laravel# php artisan greet John Smith

  Too many arguments, expected arguments "command" "who".

6 (Optional) 클래스 수정 3[ | ]

app/Console/Commands/Greet.php
PHP
Copy
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Greet extends Command
{
	protected $signature = 'greet {who*}';
	protected $description = 'Greet someone';
	public function __construct()
	{
		parent::__construct();
	}
	public function handle()
	{
		$who = implode(' ', $this->argument('who') );
		echo "Hello, $who!\n";
	}
}
실행예시
Console
Copy
root@zetawiki:/var/www/laravel# php artisan greet

  Not enough arguments (missing: "who").  

root@zetawiki:/var/www/laravel# php artisan greet John
Hello, John!
root@zetawiki:/var/www/laravel# php artisan greet John Smith
Hello, John Smith!
root@zetawiki:/var/www/laravel# php artisan greet 'John Smith'
Hello, John Smith!
root@zetawiki:/var/www/laravel# php artisan greet 'John Smith' "Jane Doe"
Hello, John Smith Jane Doe!

7 범용 args[ | ]

app/Console/Commands/Greet.php
PHP
Copy
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Greet extends Command
{
	protected $signature = 'greet {args?*}';
	protected $description = 'Greet someone';
	public function __construct()
	{
		parent::__construct();
	}
	public function handle()
	{
		print_r( $this->argument('args') );
	}
}
실행예시
Console
Copy
root@zetawiki:/var/www/laravel# php artisan greet
Array
(
)
root@zetawiki:/var/www/laravel# php artisan greet John
Array
(
    [0] => John
)
root@zetawiki:/var/www/laravel# php artisan greet John Smith
Array
(
    [0] => John
    [1] => Smith
)
root@zetawiki:/var/www/laravel# php artisan greet 'John Smith'
Array
(
    [0] => John Smith
)
root@zetawiki:/var/www/laravel# php artisan greet 'John Smith' "Jane Doe"
Array
(
    [0] => John Smith
    [1] => Jane Doe
)

8 같이 보기[ | ]

9 참고[ | ]