0

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?

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');
    }
}
Murlidhar Fichadia
  • 2,589
  • 6
  • 43
  • 93
  • @apokryfos I did but does not seem to work either :/ I created a new file HappyBirthday as per the tutorial but then HappyBirthday does not exist too. if I remove App\Console\Command\Test123::class then I can execute other commands. But is there a way to flush everything from memory? clear cache or something. – Murlidhar Fichadia Apr 12 '17 at 16:32

2 Answers2

4

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.

patricus
  • 59,488
  • 15
  • 143
  • 145
  • currently, my file Test123.php is in app/Console/Commands folder and this is my current code in kernel.php protected $commands = [ \App\Console\Command\Test123::class, ]; Now what needs to be changed? – Murlidhar Fichadia Apr 12 '17 at 16:51
  • I did fix the namespace and removed extra s and I get this error: [Symfony\Component\Console\Exception\RuntimeException] Not enough arguments (missing: "name"). – Murlidhar Fichadia Apr 12 '17 at 16:57
  • if I execute php artisan abcd then it does work and execute the code but php artisan make:list does not seem to work yet. I get above comment error – Murlidhar Fichadia Apr 12 '17 at 17:00
  • @MurlidharFichadia Since your directory is `app\Console\Commands`, then your namespace needs to be `App\Console\Commands`. Make sure your Test123 class has this namespace defined, and make sure your Kernel.php file is referencing this class with the correct full class name (`\App\Console\Commands\Test123`) – patricus Apr 12 '17 at 17:08
  • I have set everything as you said. I get php artisan abcd running perfectly fine but php artisan make:list does not seem to work probably some other issue. – Murlidhar Fichadia Apr 12 '17 at 17:11
0

I'm getting

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

How to fix above issue?

In 99,9% it's because you forgot to do

composer dumpautoload

once you created Test123 class. Do it now and it should be fine.

EDIT

The error message you are seeing shows the full name space to your class which smells to me as I am pretty much certain it should be App\Console\Command\Test123 instead. If so, edit your $commands array to make it look like this:

protected $commands = [
    \App\Console\Command\Test123::class,
];
Community
  • 1
  • 1
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • I did composer dumpautoload as u mentioned but it does not seem to work. same error – Murlidhar Fichadia Apr 12 '17 at 16:10
  • this is quite weird. I did the change as above but then I am unable to run php artisan or php artisan dump-autoload or php artisan dumpautoload. I did composer dump-autoload which worked. But I guess I am quite stuck. I cant execute php artisan serve :/ – Murlidhar Fichadia Apr 12 '17 at 16:20
  • Instead of making a file and running the schedule. I have put all my code in schedule call function. when I run it executes right on spot instead of running once every minute. How can I achieve this? when I execute schedule run. I want the task to execute after a minute. I am using ->everyMinute(); – Murlidhar Fichadia Apr 12 '17 at 16:43
  • If my answer solved your **question**, please [accept the answer](https://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work/5235#5235). If you got another question (related or not), please post a [new one](http://stackoverflow.com/questions/ask). This is how SO works. – Marcin Orlowski Apr 12 '17 at 16:47
  • It did not work, so I thought of adding all the code directly in Schedule function. but then even that has issue with executing at every minute. – Murlidhar Fichadia Apr 12 '17 at 16:53