任务调度中不存在获取错误命令Laravel

Getting error command does not exist in task scheduling Laravel

我正在学习本教程:https://www.sitepoint.com/managing-cronjobs-with-laravel/

但是当我输入命令行时 php artisan make:list 我收到一条错误消息

  [ReflectionException]
  Class App\Console\Command\Test123 does not exist

如何解决上述问题?我做错了什么?

Test123.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Carbon\Carbon;

use Response;
use Config;
use DB;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;

class Test123 extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'abcd';

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

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
           $parameters = [
        'attribute1' => 'val1',
        'attribute2' => 'val2',
        '_token' => Config::get('app.secret')
        ];
        $formattedParameters = http_build_query($parameters);
        $statusCode = 200;
        $url = "url?{$formattedParameters}"; 

        $client = new Client();
        $res = $client->get($url);

        $jsonArray = json_decode($res->getBody(),true);
        $field1= $jsonArray['field1'];

        DB::table('table_name')->insert(
    ['field1' => $field1]
);
    }
}

Kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Carbon\Carbon;

use Response;
use Config;
use DB;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;


class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
    \App\Console\Command\Test123::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {

        $schedule->command('abcd')->everyMinute();



    }

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}

I'm getting

Class App\Console\App\Console\Command\Test123 does not exist

How to fix above issue?

99.9%是因为忘记了

composer dumpautoload

一旦您创建了 Test123 class。现在就做,应该没问题。

编辑

您看到的错误消息显示了您的 class 的全名 space,这对我来说很奇怪,因为我非常确定它应该是 App\Console\Command\Test123。如果是这样,请编辑您的 $commands 数组,使其看起来像这样:

protected $commands = [
    \App\Console\Command\Test123::class,
];

你有两个问题。

首先,在您的 Kernel.php 文件中,您有:

protected $commands = [
    App\Console\Command\Test123::class,
];

由于您没有以反斜杠 (\) 开始 class 名称,它将查找相对于当前命名空间的指定 class,对于 Kernel.phpApp\Console。这就是为什么你的错误声明它找不到 class App\Console\App\Console\Command\Test123.

因此,如果您将其更改为:

protected $commands = [
    \App\Console\Command\Test123::class,
];

它现在将尝试从根命名空间中寻找 \App\Console\Command\Test123

这就引出了第二个问题。在您的 Test123 class 中,您将命名空间指定为 App\Console\Commands,而不是 App\Console\Command(您有一个额外的 s)。

如果你的文件在app\Console\Commands目录下,那么你的命名空间是正确的,你需要更正Kernel.php文件来寻找正确的class。如果您的文件在 app\Console\Command 目录中,那么您的命名空间不正确,您需要修复 Test123 class.

中的命名空间声明