当我在命令行中运行php artisan make:greetings
命令时,我想打印一个日志消息,它应该在我的日志文件中返回一条消息,对于如何在routes.php文件中调用句程方法,因此我正在编写一些代码,但我得到以下错误请帮助我解决这个问题
Error
TypeError
Argument 2 passed to IlluminateFoundationConsoleKernel::command() must be an instance of Closure, array given,
called in C:apiato-projectapiatovendorlaravelframeworksrcIlluminateSupportFacadesFacade.php on line 261
at C:apiato-projectapiatovendorlaravelframeworksrcIlluminateFoundationConsoleKernel.php:191
187▕ * @param string $signature
188▕ * @param Closure $callback
189▕ * @return IlluminateFoundationConsoleClosureCommand
190▕ */
➜ 191▕ public function command($signature, Closure $callback)
192▕ {
193▕ $command = new ClosureCommand($signature, $callback);
194▕
195▕ Artisan::starting(function ($artisan) use ($command) {
1 C:apiato-projectapiatovendorlaravelframeworksrcIlluminateSupportFacadesFacade.php:261
IlluminateFoundationConsoleKernel::command("make:greetings", ["AppConsoleCommandsHello"])
2 C:apiato-projectapiatoappShipCommandsRoutes.php:24
IlluminateSupportFacadesFacade::__callStatic("command")
routes.php
Artisan::command('make:greetings',[Hello::class,'handle'=>true]);
Hello.php
<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
use Log;
class Hello extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:greetings';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
return Log::info("welcome message");
}
}
第二个参数必须是一个闭包,并且你正在传递一个数组,这个闭包用于将数据传递给你的句柄方法,因为你的句柄方法中没有任何参数,请使用call method而不是command,试试这个:
Artisan::call('make:greetings');
,也不要忘记在AppConsoleKernel类中注册你的命令:
protected $commands = [
'AppConsoleCommandsHello',
];