安裝所需的包
composer require illuminate/console
新建一個 Command src/Commands/HelloWorldCommand.php
<?php
namespace App\Commands;
use Illuminate\Console\Command;
class HelloWorldCommand extends Command
{
protected $signature = 'hello:world';
protected $description = 'This is a simple hello world';
public function handle()
{
$this->comment('Hello World');
}
}
新建 artisan 檔案 src/artisan
#!/usr/bin/env php
<?php
require __DIR__ . '/vendor/autoload.php';
use App\Commands\HelloWorldCommand;
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Illuminate\Console\Application;
$container = new Container;
$events = new Dispatcher($container);
$artisan = new Application($container, $events, 'Version 1');
$artisan->setName('My Console App Name');
// Bind a command
$artisan->resolve(HelloWorldCommand::class);
$artisan->run();
命令列測試
php artisan
php artisan list
還缺失很多 Laravel 自帶的 artisan 命令,只能需要的時候自己一點點新增了。
本作品採用《CC 協議》,轉載必須註明作者和本文連結