2

I'm trying to use the scheduler for the first time to call a method:

protected function schedule(Schedule $schedule)
    {   
        $schedule->call('MyClassName@myMethodName')
            ->everyMinute();
    }

The class I'm calling is defined in App/Http/Controller this way:

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Models\Reaction;
use View;
use Request;

class MyClassNameController extends Controller {

But each time the scheduler runs, it gaves:

  [ReflectionException]
  Class MyClassName does not exist

How could I fix this ?

Alucard
  • 1,814
  • 1
  • 21
  • 33

1 Answers1

8

You should not call controller methods this way. Controller methods are meant for handling HTTP requests.

The content of myMethodName should be pulled out into a command. You can learn about creating commands here.

That aside, the reason you're getting the ReflectionException is because of the exact reason the exception states: MyClassName is not a valid class.

$schedule->call('App\Http\Controllers\MyClassNameController@myMethodName')

The above specifies the Fully Qualified Name of the class you are trying to refer to. You could alternatively import that class at the top of your file and use a join

use App\Http\Controllers\MyClassNameController;

// ...

$schedule->call(join('@', [ MyClassNameController::class, 'myMethodName ]))

But again, you should not be calling controller methods this way.

deefour
  • 34,974
  • 7
  • 97
  • 90
  • Thank you. I don't want to create a CLI command though, I just want to execute a method every minute... – Alucard Apr 22 '15 at 14:40
  • 1
    I'm not suggesting you to create a CLI command. I am suggesting you create a class outside the context of your controller. A class that can be queued, called from the command line, dispatched from within a controller action, or run via the scheduler. Putting this method directly in a controller is not the right way to go about this. – deefour Apr 22 '15 at 14:52
  • I see what you mean, I already started making it, and it make perfect sense, thank you – Alucard Apr 22 '15 at 14:56