Laravel 5“类不存在”当使用调度程序时 [英] Laravel 5 "Class does not exist" when using the scheduler

查看:237
本文介绍了Laravel 5“类不存在”当使用调度程序时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图第一次使用调度程序来调用方法:

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();
    }

我调用的类定义在 / Http / Controller 这样:

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 {

但是每次调度程序运行时gaves:

But each time the scheduler runs, it gaves:

  [ReflectionException]
  Class MyClassName does not exist

如何解决这个问题?

推荐答案

不是这样调用控制器方法。控制器方法用于处理HTTP请求。

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

myMethodName 的内容应该被拉出到命令中。您可以了解如何在此创建命令

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

除此之外,你得到 ReflectionException 的原因是因为异常声明的确切原因: MyClassName 是不是有效的类。

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 ]))

但是,您不应以这种方式调用控制器方法

这篇关于Laravel 5“类不存在”当使用调度程序时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆