繁体   English   中英

创建Laravel软件包,但未在Artisan上列出命令

[英]Create Laravel Package but Command not Listed On Artisan

我一直在创建简单的包,这是我的目录结构,并使用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"
            ]
        }
    }
}

并在laravel项目上的composer.json中添加了我:

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

我是否想念某些东西,使我的自定义命令未在artisan命令上列出?

从laravel 文档中 ,您应该将command方法调用放在服务提供商的boot方法中。
我还将重命名您的command变量或完全删除它,以避免在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,
            ]);
        }
    }

    // ...
}

注意:如果使用robyfirnandoyusuf -4命名空间TestMe自动加载软件包,则在使用它时,不应将robyfirnandoyusuf在命名空间之前。


更新从聊天中我们发现类名称空间不正确,因为提供程序不在Commands文件夹中。 相应地对其进行更新,并使用composer.json进行相同操作即可解决此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM