Laravel 运行 来自控制台命令的函数
Laravel Run function from console command
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 路由方法有效时,但我想 运行 通过控制台命令。这是我在控制台上的命令:php artisan helloworld。我得到错误:
[Symfony\Component\Debug\Exception\FatalErrorException]Call to undefined method App\Console\Commands\HelloWorldCommand::helloWorld()
我的问题是:有什么方法可以通过命令控制台调用这个函数吗?如何?
先感谢您!
已解决!
我刚刚放置了句柄控制器的 class 名称并调用了如下函数:
$x = new HelloWorldController();
echo $x->helloWorld();
成功了!
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 路由方法有效时,但我想 运行 通过控制台命令。这是我在控制台上的命令:php artisan helloworld。我得到错误:
[Symfony\Component\Debug\Exception\FatalErrorException]Call to undefined method App\Console\Commands\HelloWorldCommand::helloWorld()
我的问题是:有什么方法可以通过命令控制台调用这个函数吗?如何? 先感谢您!
已解决! 我刚刚放置了句柄控制器的 class 名称并调用了如下函数:
$x = new HelloWorldController();
echo $x->helloWorld();
成功了!