2017-02-15 161 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(); 

它的工作!