Laravel Run功能来自console命令

时间:2017-02-15 09:47:55

标签: laravel controller console command execute

LARAVEL 5.2,刚刚创建了名为“HelloWorld”的命令,这里是代码:

 <?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';
    }

}

到目前为止,我的Kernel.php有以下命令:

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

当我运行控制器VIA Routing方法时,它可以工作,但我想通过Console命令运行它。这是我在控制台上的命令:php artisan helloworld。我收到错误:

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

我的问题是:有没有办法调用此功能VIA命令控制台?怎么样? 先感谢您!

1 个答案:

答案 0 :(得分:2)

解决! 我刚刚放置了句柄控制器的类名,并将该函数调用如下:

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

有效!

相关问题