I need code from my controller to run every ten minutes. Easy enough with Scheduler and Commands . But. I created a Command , registered it with the Laravel Scheduler (in Kernel.php ), and now I cannot create an instance of Controller . I know that this is the wrong approach to solving this problem, but I just needed a quick test. Is there a way, of course, a hacked way to achieve this? Thanks.
Update # 1:
Command :
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use App\Http\Controllers\StatsController; class UpdateProfiles extends Command { protected $signature = 'update-profiles'; protected $description = 'Updates profiles in database.'; public function __construct() { parent::__construct(); } public function handle() { StatsController::updateStats('<theProfileName>'); } }
updateStats() in StatsController.php
public static function updateStats($theProfileName) {
This returns a FatalErrorException :
[Symfony\Component\Debug\Exception\FatalErrorException] syntax error, unexpected 'if' (T_IF)
Update # 2:
Turns out I had a typo in the updateStats() method, but @ alexey-mezenin's answer works like a charm! It is also sufficient to import the Controller into Command :
use App\Http\Controllers\StatsController;
And then initialize it as usual:
public function handle() { $statControl = new StatsController; $statControl->updateStats('<theProfileName>'); }
source share