在非 laravel 项目中使用 laravel 的特性 6: artisan
artisan
安装所需的包
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 协议》,转载必须注明作者和本文链接
推荐文章: