2017-02-15 29 views
1

LARAVEL 5.2,只是創建了一個名爲 「HelloWorld」 的,這裏的命令是代碼:從控制檯命令Laravel運行功能

<?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(); 

    } 
} 

我控制器HelloWorldController.php看起來如下:

<?php 

namespace App\Http\Controllers; 

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

} 

My Kernel.php到目前爲止有以下命令:

protected $commands = [ 
     Commands\Inspire::class, 
     Commands\HelloWorldCommand::class, 
    ]; 

當我運行控制器威盛路由方法它的工作原理,但我想通過控制檯命令運行此。這是我在控制檯上的命令:php artisan helloworld。而我得到的錯誤:

[Symfony\Component\Debug\Exception\FatalErrorException]Call to undefined method App\Console\Commands\HelloWorldCommand::helloWorld() 

我的問題是:有沒有辦法來調用此功能通過命令控制檯?怎麼樣? 提前謝謝!

回答

1

解決! 我剛剛放在手柄控制器的類名和調用的函數如下:

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

它的工作!