Laravel 5“类不存在"使用调度程序时 [英] Laravel 5 "Class does not exist" when using the scheduler
问题描述
我第一次尝试使用调度程序来调用一个方法:
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();
}
我正在调用的类在 App/Http/Controller
中是这样定义的:
The class I'm calling is defined in App/Http/Controller
this way:
namespace AppHttpControllers;
use AppHttpRequests;
use AppModelsReaction;
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 ?
推荐答案
您不应该以这种方式调用控制器方法.控制器方法用于处理 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('AppHttpControllersMyClassNameController@myMethodName')
上面指定了您尝试引用的类的完全限定名称.您也可以在文件顶部导入该类并使用 join
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 AppHttpControllersMyClassNameController;
// ...
$schedule->call(join('@', [ MyClassNameController::class, 'myMethodName ]))
但同样,您不应该以这种方式调用控制器方法.
这篇关于Laravel 5“类不存在"使用调度程序时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!