0

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

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

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

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

测试123.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]
);
    }
}

内核.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');
    }
}
4

2 回答 2

4

你有两个问题。

首先,在你的Kernel.php文件中,你有这个:

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

由于您没有以反斜杠 ( \) 开头类名,因此它将查找相对于当前命名空间的指定类,即 for Kernel.phpis App\Console。这就是为什么您的错误表明它找不到类的原因App\Console\App\Console\Command\Test123

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

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

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

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

如果您的文件在app\Console\Commands目录中,那么您的命名空间是正确的,您需要更正Kernel.php文件以查找正确的类。如果您的文件在app\Console\Command目录中,那么您的命名空间不正确,您需要修复Test123类中的命名空间声明。

于 2017-04-12T16:45:57.927 回答
0

我越来越

类 App\Console\App\Console\Command\Test123 不存在

如何解决上述问题?

99.9% 是因为你忘了做

composer dumpautoload

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

编辑

您看到的错误消息显示了您的班级的全名空间,这对我来说很臭,因为我几乎可以肯定它应该是App\Console\Command\Test123。如果是这样,请编辑您的$commands数组以使其看起来像这样:

protected $commands = [
    \App\Console\Command\Test123::class,
];
于 2017-04-12T16:07:49.643 回答