【问题标题】:Create Laravel Package but Command not Listed On Artisan创建 Laravel 包但命令未在 Artisan 上列出
【发布时间】:2019-08-30 21:52:02
【问题描述】:

我已经创建了简单的包,这里是我的目录结构并使用 laravel 5.4

├── composer.json
└── src
    ├── ArtisanTestmeServiceProvider.php
    └── Commands
        └── TestmeCommand.php

我的问题是,我的自定义命令未在 artisan (php artisan) 中列出

这里是TestMeCommand.php

的源代码
<?php

namespace TestMe\Commands;

use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'lorem ipsum';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $this->info("Version : test test");
    }
}

这里是 ServiceProvider ArtisanTestmeServiceProvider

<?php

namespace TestMe\Commands;

use Illuminate\Support\ServiceProvider;

class ArtisanTestmeServiceProvider extends ServiceProvider
{
    protected $commands = [
        'robyfirnandoyusuf\TestMe\Commands\TestmeCommand',
    ];


    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->commands($this->commands);
    }
}

以下是我的包中的composer.json

{
    "name": "robyfirnandoyusuf/lara-testme",
    "description": "test",
    "license": "MIT",
    "authors": [
        {
            "name": "Roby Firnando Yusuf",
            "email": "idonthave@gmail.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {},
    "autoload" : {
        "psr-4":{
            "TestMe\\": "src/"
        }
    },
    "extra":{
        "laravel":{
            "providers":[
                "TestMe\\Commands\\ArtisanTestmeServiceProvider"
            ]
        }
    }
}

在 composer.json on laravel 项目,我已被添加:

 "repositories":[
        {
            "type": "path",
            "url": "../Packagistku/TestPackage/"
        }
    ],

我是否错过了一些东西,以至于我的自定义命令未列在 artisan command 中?

【问题讨论】:

  • 可能是由于这里的命令数组中的单引号:'robyfirnandoyusuf\TestMe\Commands\TestmeCommand' 只需删除它并尝试
  • 我改成双引号并去掉引号但还是没有列出...
  • 我的意思是不要添加任何报价 ;)
  • 还是不行……
  • 如果你运行命令 php artisan testme:run 它是否工作?

标签: php laravel laravel-5 package


【解决方案1】:

从 laravel documentation,您应该将 commands 方法调用放在服务提供者的 boot 方法中。
我还将重命名您的 commands 变量或完全删除它,以避免在 commands 数组和 commands 方法之间产生任何混淆:

// Do not forget the use statement at the top
use TestMe\Commands\TestmeCommand;

class ArtisanTestmeServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        if ($this->app->runningInConsole()) {
            $this->commands([
                TestmeCommand::class,
            ]);
        }
    }

    // ...
}

注意:如果您使用 psr-4 命名空间 TestMe 自动加载包,则在使用时不应将 robyfirnandoyusuf 添加到命名空间。


更新 从聊天中我们发现类命名空间不正确,因为提供程序不在 Commands 文件夹中。相应地更新它并对 composer.json 做同样的事情解决了这个问题。

【讨论】:

  • 当你从项目的根目录(不是包的根目录)做composer dump-autoload时,它是否告诉你你的包的服务提供者已经被识别并成功加载了?
  • 输出是生成优化的自动加载文件
  • 顺便说一下我用的是laravel 5.4……没关系吗?
  • 原来是这样,laravel 5.5 已经引入了自动发现包
  • 哦,好吧伙计..让我先升级它
猜你喜欢
  • 2015-04-14
  • 2014-04-01
  • 2018-02-21
  • 2019-04-02
  • 2015-12-24
  • 1970-01-01
  • 2016-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多