php - 如何将句柄方法调用到 routes.php 文件中?
问题描述
我想php artisan make:greetings
在命令行中运行命令时打印一条日志消息,它应该在我的日志文件中返回一条消息,说明如何在 routes.php 文件中调用句柄方法,因为我正在编写一些代码,但我是收到以下错误请帮助我解决此问题
Error
TypeError
Argument 2 passed to Illuminate\Foundation\Console\Kernel::command() must be an instance of Closure, array given,
called in C:\apiato-project\apiato\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php on line 261
at C:\apiato-project\apiato\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php:191
187▕ * @param string $signature
188▕ * @param \Closure $callback
189▕ * @return \Illuminate\Foundation\Console\ClosureCommand
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-project\apiato\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:261
Illuminate\Foundation\Console\Kernel::command("make:greetings", ["App\Console\Commands\Hello"])
2 C:\apiato-project\apiato\app\Ship\Commands\Routes.php:24
Illuminate\Support\Facades\Facade::__callStatic("command")
routes.php
Artisan::command('make:greetings',[Hello::class,'handle'=>true]);
Hello.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
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");
}
}
解决方案
第二个参数必须是一个闭包,并且您正在传递一个数组,此闭包用于将数据传递给您的句柄方法,并且由于您的句柄方法中没有任何参数,请使用调用方法而不是命令,试试这个:
Artisan::call('make:greetings');
并且不要忘记在 App\Console\Kernel 类中注册您的命令:
protected $commands = [
'App\Console\Commands\Hello',
];
推荐阅读
- c - C编程,使用报警
- php - Laravel foreach 似乎停在了第一条记录上
- javascript - HTML5 中的拖放操作
- c++ - 如何在不使用委托的情况下在 QML 中显示模型数据
- c# - 检测到.Net Core 3.0 可能的对象循环,但不支持
- powershell - Get-ADUser - 从 SZ 中过滤姓氏,以反向 alpha 顺序列出
- python - 如何计算每个嵌套列表的项目频率?
- php - 如何限制对 html 文件的访问
- android - Android Studio - 无法解析 com.android.tools.build:gradle:3.5.2
- postgresql - Google PostGIS - ogr2ogr 无法打开数据源