티스토리 뷰
반응형
Laravel에서 Command 기능은 Artisan 콘솔 명령어를 생성하고 실행할 수 있게 해주는 기능. Artisan은 Laravel의 강력한 CLI(Command Line Interface) 도구로, 커맨드를 만들어 개발 프로세스를 자동화하거나 반복 작업을 간편하게 수행할 수 있게 도와줌.
✅ Laravel Command 기본 개념
Laravel에서는 커맨드를 php artisan make:command 명령어로 생성해요. 생성된 커맨드는 app/Console/Commands 디렉터리에 저장되며, Artisan을 통해 실행할 수 있음.
📦 Command 생성 방법
php artisan make:command CustomCommandName
예)
php artisan make:command SendEmails
🧱 Command 구조
생성된 파일 예시 (app/Console/Commands/SendEmails.php):
namespace App\Console\Commands;
use Illuminate\Console\Command;
class SendEmails extends Command
{
// Artisan 명령어 이름
protected $signature = 'emails:send';
// 설명 (php artisan list에서 보임)
protected $description = 'Send emails to users';
public function __construct()
{
parent::__construct();
}
public function handle()
{
// 실제 로직
$this->info('Sending emails...');
// 예: Mail::to($user)->send(new Notification());
}
}
🏃 실행 방법
php artisan emails:send
✍️ 옵션 & 인자 사용하기
protected $signature = 'emails:send {userId} {--queue}';
public function handle()
{
$userId = $this->argument('userId');
$queue = $this->option('queue');
$this->info("Sending email to user $userId, queue: " . ($queue ? 'yes' : 'no'));
}
🔄 스케줄링과 연동 (선택사항)
app/Console/Kernel.php에서 커맨드를 자동으로 주기적으로 실행할 수도 있어요.
protected function schedule(Schedule $schedule)
{
$schedule->command('emails:send')->daily();
}
✨ 요약
요소설명
php artisan make:command | 커맨드 생성 |
$signature | 명령어 이름 및 옵션 정의 |
handle() | 실행 로직 |
php artisan your:command | 실행 방법 |
Kernel에 등록 | 스케줄링 가능 |
반응형
'배움 > PHP' 카테고리의 다른 글
Laravel 11 라라벨 스케줄링(Scheduling) (0) | 2025.04.16 |
---|---|
Lavavel Console Command Argument 옵션 (0) | 2025.04.15 |
Laravel 의 Concurrency 기능 (0) | 2025.04.14 |
PHP Carbon (1) | 2025.04.11 |
Laravel의 Scope 기능 (0) | 2025.04.11 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 설정
- researcher
- call_user_func
- mysql
- facades
- swagger
- array_combine
- php
- createfromformat
- strict_types
- ob_get_contents
- curl_multi_init
- eloquent
- wsl
- laravel 12
- WSL2
- Laravel
- flask
- 명령어
- PYTHON
- jp:a
- 비동기
- #collect
- 설치
- reflectionclass
- ubuntu
- uniqid
- privatechannel
- laravel 11
- laravel 테스트
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
글 보관함