關於網路那些事...

網路行銷,SEO,網路趨勢,教學文章,網頁設計,生活時事

Laravel - How to Custom a Artisan Console

There show how to custom a php artisan command

first create your custom command

This will auto generate a file in app/Console/Command/Tests/TestOutput.php

and setting a relative command for calling this file in terminal command

 php artisan make:command Tests/TestOutput --command=test:test_output

A new file will generated in app/Console/Command/Tests/TestOutput.php

and we add a message in handle.

<?php

namespace App\Console\Commands\Tests;

use Illuminate\Console\Command;

class TestOutput extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:test_output';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This is a Test Command description';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $this->line('Display this test line on the screen');

    }
}

Check command already builded

Show all laravel artisan commands:

php artisan 

and you will see custom cummand in list result.

...
app/public"

 test

  test:test_output     Command description

...

Regist your command to Kernel.php
app/Console/Kernel.php

protected $commands = [
    'App\Console\Commands\Tests'
];

Finally! You can using the custom command

php artisan test:test_output

如果你喜歡我們的文章內容,請在這裡按個讚



最新文章推薦