Laravel 5“Class不存在”当使用调度程序时 [英] Laravel 5 "Class does not exist" when using the scheduler
问题描述
我第一次尝试使用调度程序调用一个方法:
保护功能计划(Schedule $日程表)
{
$ schedule-> call('MyClassName @ myMethodName')
- > everyMinute();
}
我打电话的类是在 App中定义的/ Http / Controller
这样:
命名空间App \Http\Controllers;
使用App \Http\Requests;
使用App \Models\Reaction;
使用View;
使用请求;
class MyClassNameController extends Controller {
但每次调度程序运行时, gaps:
[ReflectionException]
/ pre>
Class MyClassName不存在
我如何解决这个问题?
解决方案不这样调用controller方法。控制器方法用于处理HTTP请求。
应该将
myMethodName
的内容拉出到命令中。您可以了解如何在此创建命令。
除此之外,你得到
ReflectionException
的原因是因为异常声明的确切原因:MyClassName
不是一个有效的类。$ schedule-> call('App \Http\Controllers\\ \\ MyClassNameController @ myMethodName')
上面指定了您尝试引用的类的完全限定名称至。您也可以在文件顶部导入该类,并使用
加入
使用App\Http\Controllers\MyClassNameController;
// ...
$ schedule-> call(join('@',[MyClassNameController :: class,'myMethodName]))
但是,再一次,你不应该这样调用控制器方法。
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 ?
解决方案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.
这篇关于Laravel 5“Class不存在”当使用调度程序时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!