2017-02-15 7 views
1

Laravel 5.2, erstellt einfach den Befehl namens "Hello World" und hier ist der Code:Laravel Run Funktion von Konsolenbefehl

<?php 

namespace App\Console\Commands; 

use Illuminate\Console\Command; 
use App\Http\Controllers\HelloWorldController; 

class MakeImportsCommand extends Command 
{ 
    /** 
    * The name and signature of the console command. 
    * 
    * @var string 
    */ 
    protected $signature = 'helloworld'; 

    /** 
    * The console command description. 
    * 
    * @var string 
    */ 
    protected $description = 'Say Hello World Controller'; 

    /** 
    * Create a new command instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     parent::__construct(); 
    } 

    /** 
    * Execute the console command. 
    * 
    * @return mixed 
    */ 
    public function handle() 
    { 
     return $this -> helloWorld(); 

    } 
} 

Mein Controller HelloWorldController.php wie unten aussieht:

<?php 

namespace App\Http\Controllers; 

class HelloWorldController extends Controller 
{ 
    public function helloWorld() { 
     echo 'Hello World from controller'; 
    } 

} 

Meine Kernel.php hat folgende Befehle bis jetzt:

Wenn ich die Controller-VIA-Routing-Methode ausführe, funktioniert es, aber ich möchte dies über den Konsolenbefehl ausführen. Hier ist mein Befehl auf der Konsole: php artisan helloworld. Und ich bekomme den Fehler:

Meine Frage ist: Gibt es eine Möglichkeit, diese Funktion VIA Befehlskonsole aufzurufen? Wie? Vielen Dank im Voraus!

Antwort

1

Gelöst! habe ich nur auf Klasse Griff Controller den Namen gegeben und nannte die Funktion, wie folgend:

$x = new HelloWorldController(); 
echo $x->helloWorld(); 

Es hat funktioniert!