Laravel Artisan

이하루·2024년 7월 21일

Artisan이란 Laravel에서 제공하는 CLI(=내부 실행 콘솔)로, Laravel개발에 도움을 주는 많은 명령어를 제공한다.

💻 주요 명령어

php artisan list : 제공하는 artisan 명령어의 리스트를 보여준다.

php artisan serve : 라라벨을 구동하기 위한 서버를 실행한다.

php artisan migrate : 데이터베이스 마이그레이션 실행, DB에 관리할 테이블 전용 클래스를 만듬

php artisan make:model : 새로운 모델 클래스 생성

php artisan make:controller : 새로운 컨트롤러 클래스 생성

php artisan route:list : 설정된 루트 리스트 확인

💾 커스텀 명령어 생성

제공해주는 명령어 이외에도 사용자가 직접 명령어와 실행시 필요한 동작을 정의할 수 있는 커스텀 명령어 생성 기능이 있다. 아래와 같이 새로운 명령어를 추가할 때 필요한 절차를 알아보자.

1. artisan CLI에 아래의 명령어를 실행

php artisan make:command TestCommand

실행 시, 아래와 같은 클래스가 app/Console/Commands 아래에 생성된다.

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class TestCommand extends Command
{
    // 명령어 등록 시, 사용되는 식별자로, 뒤에 {}를 추가하여 인자를 받을 수도 있음. 
    protected $signature = 'test:command {testData}';

    // 명령어에 대한 설명
    protected $description = 'this is test command!';

	// 생성자
    public function __construct()
    {
        parent::__construct();
    }

	// 명령어를 실행 시, 발생하는 처리
    public function handle()
    {
    	// 명령어 작성 중 받아올 인자를 지정했을 경우
        $user = $this->argument('testData');
        return 0;
    }
}

2. Command클래스를 artisan명령어로 등록

app/Console/Kernel.php의 $commands에 클래스를 등록한다.

    protected $commands = [
        Commands\TestCommand::class
    ];

3. 명령어 실행

php artisan test:command {username}
profile
어제보다 더 나은 하루

0개의 댓글