Laravel 5 - how to run Controller method from Artisan command?

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 { /** * 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() in StatsController.php

 public static function updateStats($theProfileName) { // the body } 

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>'); } 
+6
source share
1 answer

Try using use Full\Path\To\Your\Controller; in your batch code and use the method statically:

 public static function someStaticMethod() { return 'Hello'; } 

In the command code:

 echo myClass::someStaticMethod(); 
+5
source

All Articles