I am following this tutorial: https://www.sitepoint.com/managing-cronjobs-with-laravel/
but when I type in command line php artisan make:list
i get an error message
[ReflectionException]
Class App\Console\Command\Test123 does not exist
How to fix above issue? What am I doing wrong?
<?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
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');
}
}
You have two issues.
First, in your Kernel.php
file, you have this:
protected $commands = [
App\Console\Command\Test123::class,
];
Since you didn't start the class name with a backslash (\
), it will look for the specified class relative to the current namespace, which for Kernel.php
is App\Console
. That is why your error states it can't find the class App\Console\App\Console\Command\Test123
.
So, if you change that to:
protected $commands = [
\App\Console\Command\Test123::class,
];
It will now attempt to look for \App\Console\Command\Test123
from the root namespace.
That leads to the second issue. In your Test123
class, you have the namespace specified as App\Console\Commands
, not App\Console\Command
(you have an extra s
).
If your file is in the app\Console\Commands
directory, then your namespace is correct, and you need to correct the Kernel.php
file to look for the correct class. If your file is in the app\Console\Command
directory, then your namespace is incorrect, and you need to fix the namespace declaration in your Test123
class.