我需要控制器中的一些代码每十分钟运行一次。 Scheduler
和Commands
足够简单。但。我已经创建了Command
,在Laravel Scheduler
注册了它(在Kernel.php
中),现在我无法实例化Controller
。我知道解决这个问题的方法不对,但我只需要快速测试。为了达到这个目的,有没有办法,请注意一个黑客的方式?谢谢。
更新#1:
Command
:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Http\Controllers\StatsController;
class UpdateProfiles extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'update-profiles';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Updates profiles in database.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
StatsController::updateStats('<theProfileName>');
}
}
updateStats()
中的 StatsController.php
方法
public static function updateStats($theProfileName) {
// the body
}
这会返回FatalErrorException
:
[Symfony\Component\Debug\Exception\FatalErrorException]
syntax error, unexpected 'if' (T_IF)
更新#2:
事实证明我在updateStats()
方法中有一个拼写错误,但@ alexey-mezenin的回答就像一个魅力!将Controller
导入Command
:
use App\Http\Controllers\StatsController;
然后在你正常做的时候初始化它:
public function handle() {
$statControl = new StatsController;
$statControl->updateStats('<theProfileName>');
}
答案 0 :(得分:5)
尝试在命令代码中使用use Full\Path\To\Your\Controller;
并静态使用方法:
public static function someStaticMethod()
{
return 'Hello';
}
在您的命令代码中:
echo myClass::someStaticMethod();