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

时间:2017-04-12 16:06:10

标签: php laravel laravel-5.3 laravel-scheduler

我正在关注本教程: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');
    }
}

2 个答案:

答案 0 :(得分:1)

你有两个问题。

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

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

由于您没有使用反斜杠(\)启动类名,因此它将查找相对于当前命名空间的指定类,Kernel.php为{{1} }。这就是为什么您的错误表明它无法找到班级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类中修复名称空间声明。

答案 1 :(得分:0)

  

我正在

     

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

     

如何解决上述问题?

99.9%因为你忘了做

composer dumpautoload

创建Test123课程后。现在就做,应该没事。

修改

您看到的错误消息显示了您的课程的全名空间,这对我来说很难闻,因为我非常确定它应该是App\Console\Command\Test123。如果是这样,请编辑您的$commands数组,使其如下所示:

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