7

I need some code from my Controller to run every ten minutes. Easy enough with Scheduler and Commands. But. I've created a Command, registered it with Laravel Scheduler (in Kernel.php) and now I am unable to instantiate the Controller. I know it's a wrong way to approach this problem, but I just needed a quick test. Is there a way, mind you a hacky way, to accomplish this? Thank you.

Update #1:

The 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() method 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 that I've had an typo in the updateStats() method, but the answer by @alexey-mezenin works like a charm! It is also enough to import the Controller into the Command:

use App\Http\Controllers\StatsController;

And then initialize it as you'd do normally:

public function handle() {
   $statControl        = new StatsController;
   $statControl->updateStats('<theProfileName>');
}
iSS
  • 2,598
  • 2
  • 14
  • 17
  • You could map a route to the controller method and use [this command](http://stackoverflow.com/questions/28866821/call-laravel-controller-via-command-line/28868350#28868350) to run the route path. – Bogdan Apr 09 '16 at 15:37
  • Why not do it the right way move the code you need to run out of the controller and call it from a command/controller respectively. Much cleaner than a hacky solution. – stoppert Apr 10 '16 at 09:51
  • I fully agree, but when you encounter the stuff for the first time and just want to learn, you inevitably use hacky solutions. After you learn enough, understand the concepts, you refactor it. – iSS Apr 10 '16 at 11:02

1 Answers1

5

Try to use use Full\Path\To\Your\Controller; in your command code and use method statically:

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

In your command code:

echo myClass::someStaticMethod();
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • Spasibo. I've tried it, but unfortunatelly, it didn't work. This is what it returned: `[Symfony\Component\Debug\Exception\FatalErrorException] syntax error, unexpected 'if' (T_IF)` – iSS Apr 09 '16 at 15:31
  • I mean the code with 'unexpected if'. You have syntax error in a controller or other class. – Alexey Mezenin Apr 09 '16 at 16:06
  • I think this exception comes straight out of Symfony, how can I determine which file is it coming from? When I run the command using Artisan, I only get that, nothing else, no stack trace. – iSS Apr 09 '16 at 16:08
  • Check Laravel logs, maybe you'll find trace there. Also check all last changes you made. – Alexey Mezenin Apr 09 '16 at 16:13
  • Aww. Wow. A semicolon wasn't there in the end, you're right, just a typo in the updateStats() method. It works now! Thank you! – iSS Apr 09 '16 at 16:16
  • I'm glad I could help, comrade. ) – Alexey Mezenin Apr 09 '16 at 16:23
  • @AlexeyMezenin hi, I encountered the same problem and my error log: [Symfony\Component\Debug\Exception\FatalThrowableError] Using $this when not in object context. How can I solve it? – horse Sep 10 '17 at 05:07