Laravel 5 - Wie führe ich eine Controller-Methode mit einem Artisan-Befehl aus?
Ich benötige Code von meinem Controller, um alle zehn Minuten ausgeführt zu werden. Einfach genug mitScheduler
undCommands
. Aber. Ich habe ein @ erstelCommand
, registriert es mitLaravel Scheduler
(imKernel.php
) und jetzt kann ich das @ nicht instanziierController
. Ich weiß, es ist ein falscher Weg, um dieses Problem anzugehen, aber ich brauchte nur einen kurzen Test. Gibt es einen Weg, wohlgemerkt, dies zu erreichen? Vielen Dank
Update # 1:
DasCommand
:
<?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()
Methode inStatsController.php
public static function updateStats($theProfileName) {
// the body
}
Dies gibt ein @ zurüFatalErrorException
:
[Symfony\Component\Debug\Exception\FatalErrorException]
syntax error, unexpected 'if' (T_IF)
Update # 2:
Es stellt sich heraus, dass ich einen Tippfehler im @ hatupdateStats()
Methode, aber die Antwort von @ alexey-mezenin funktioniert wie ein Zauber! Es reicht auch aus, das @ zu importierController
in dasCommand
:
use App\Http\Controllers\StatsController;
Und initialisieren Sie es dann wie gewohnt:
public function handle() {
$statControl = new StatsController;
$statControl->updateStats('<theProfileName>');
}