LoginSignup
5
6

More than 5 years have passed since last update.

【Laravel(Lumen)】Artisanコマンドで実行できる処理(バッチ)を実装する

Last updated at Posted at 2017-12-21

cronで動かすようにもできるよう
タスクスケジュール 5.4 Laravel

1.処理クラスの作成

app\Console\Commands 以下にクラスを作成
※Lumenはmakeコマンドが使えないので手動で作成する

以下例
app\Console\Commands\BatchCommand.php

BatchCommand.php
<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;

class BatchCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
     protected $signature = 'batch-execute'; // コマンド名

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'description'; // コマンドの説明

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // 処理
    }

}

2.クラス追加と実行コマンド記述

Kernel.phpにクラスを追加してscheduleメソッドに実行するコマンドを記述する

app\Console\Kernel.php

Kernel.php
<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        \Laravelista\LumenVendorPublish\VendorPublishCommand::class
        ,\App\Console\Commands\BatchCommand::class, // クラスを追加
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // コマンド実行のスケジューリング設定等
        $schedule->command('batch-execute');
    }
}

3.コマンド実行

sshで php artisan [コマンド] を実行する

vagrant@project:~/***$ php artisan batch-execute
vagrant@project:~/***$

以上。

ちなみにBatchCommandクラスのdescription プロパティに設定した説明は以下のように表示される

vagrant@project:~/***$ php artisan list
Laravel Framework Lumen (5.5.2) (Laravel Components 5.5.*)

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  batch-execute       description // コマンドの説明はここに表示される
  help                Displays help for a command
  list                Lists commands
  migrate             Run the database migrations
  tinker              Interact with your application
 auth
  auth:clear-resets   Flush expired password reset tokens
 cache
  cache:clear         Flush the application cache
 ~~~ 略 ~~~
vagrant@project:~/***$

参考

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6