如何写一个 Laravel 的 Artisan 命令行工具?

Artisan 是 Laravel 自带的命令行接口,它提供了许多实用的命令来帮助你构建 Laravel 应用

开始接触 Laravel 这个框架的时候,才发现竟然可以使用命令行去执行一些操作,比如:创建文件,运行一个服务等.出于学习或者不能满足需求的时候,我们就需要自己去写一个 Artisan 命令行。

使用命令行输出 Hello
  • 在项目的根目录下面执行 php artisan make:command Hello。该命令的结果会在 app\Console 下面创建一个 Commands 的文件夹,并且创建 Hello.php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class Hello extends Command
{
    /**
     * 控制台命令 signature 的名称。
     *
     * @var string
     */
    protected $signature = 'hello';

    /**
     * 控制台命令说明
     *
     * @var string
     */
    protected $description = '这条命令将会输出一个 hello';

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

    /**
     * 执行命令
     *
     * @return mixed
     */
    public function handle()
    {
        var_dump('Hello');
    }
}
  • app/Console/Commands 下面的命令都会自动注册到 Artisan,看这个文件app/Console/Kernel.php
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }

你也可以调用 load 方法添加你的其他 Commands 文件夹

  • 执行 php artisan

clipboard.png

  • 执行 php artisan hello

clipboard.png
这样就很简单的写出了第一个 Artisan 命令行

使用 Artisan 启动一个服务
  • 我们创建一个服务命令 php artisan make:command SwooleStart
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '启动 swoole';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $serv = new \swoole_server("127.0.0.1", 9501);

        //监听连接进入事件
        $serv->on('connect', function ($serv, $fd) {
            echo "Client: Connect.\n";
        });

        //监听数据接收事件
        $serv->on('receive', function ($serv, $fd, $from_id, $data) {
            $serv->send($fd, "Server: ".$data);
        });

        //监听连接关闭事件
        $serv->on('close', function ($serv, $fd) {
            echo "Client: Close.\n";
        });

        //启动服务器
        $serv->start();
    }
}
  • 执行 php artisan swoole:start

clipboard.png

  • 在打开一个命令行窗口 输入telnet 127.0.0.1 9501 用来监听这个端口,

clipboard.png

这样就成功的使用 Artisan 启动了一个服务。

当然你也可以询问是否启动

  • 使用 ask 方法
public function handle()
    {
        if ($this->ask('是否启动 swlloe,请输入 yes') != 'yes') {
            die;
        }
        $serv = new \swoole_server("127.0.0.1", 9501);

        //监听连接进入事件
        $serv->on('connect', function ($serv, $fd) {
            echo "Client: Connect.\n";
        });

        //监听数据接收事件
        $serv->on('receive', function ($serv, $fd, $from_id, $data) {
            $serv->send($fd, "Server: " . $data);
        });

        //监听连接关闭事件
        $serv->on('close', function ($serv, $fd) {
            echo "Client: Close.\n";
        });

        //启动服务器
        $serv->start();
    }

clipboard.png

像 Artisan 那样创建文件
  • 我们先创建一个命令行文件 php artisan make:MakeController
  • 修改继承 Commanduse Illuminate\Console\GeneratorCommand;
<?php

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;


class MakeController extends GeneratorCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'controller:make';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new controller class';



    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {

        return __DIR__.'/stubs/controller.stub';
    }

    /**
     * Get the default namespace for the class.
     *
     * @param  string  $rootNamespace
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace.'\Http\Controllers';
    }

}
  • app\Console\commands下创建一个模板目录 stubs,里面存放要生成文件的模板,创建 controller.stub
<?php

namespace DummyNamespace;

use Illuminate\Http\Request;
use DummyRootNamespaceHttp\Controllers\Controller;

class DummyClass extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

}

在执行 Artisan 是会将 DummyClass 等以 Dummy 开头的替换为你的参数,替换代码可以看 GeneratorCommand,

  • getDefaultNamespace 修改你的文件存放目录
  • getStub是必须实现的方法。
  • 执行 php artisan controller:make HelloController 你将会在 Http\controller 下面看到你使用命令行创建的文件。
  • 创建文件的 handle 在继承的 GeneratorCommand 里面写好了,如果你还需要执行一些其他操作,在当前 command 里面写就好了。
  • Artlsan 还可以携带参数,还有一些其他的小方法,可以参考 Laravle 的文档。

Laravel 的 Artisan 命令行工具

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值