首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在routes.php文件中调用handle方法?

如何在routes.php文件中调用handle方法?
EN

Stack Overflow用户
提问于 2021-08-23 06:08:31
回答 1查看 72关注 0票数 0

我想打印一条日志消息,当我在我的命令行中运行php artisan make:greetings命令时,它应该在我的日志文件中返回一条消息,关于如何在routes.php文件中调用句柄方法,因为我正在编写一些代码,但我得到了以下错误,请帮助我解决这个问题

Error

代码语言:javascript
复制
   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

代码语言:javascript
复制
 Artisan::command('make:greetings',[Hello::class,'handle'=>true]);

Hello.php

代码语言:javascript
复制
<?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");
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-23 07:21:48

第二个参数必须是一个闭包,并且你正在传递一个数组,这个闭包用于将数据传递给你的handle方法,并且当你的handle方法中没有任何参数时,使用call方法而不是command,试试这个:

代码语言:javascript
复制
Artisan::call('make:greetings');

另外,不要忘记在App\Console\Kernel类中注册命令:

代码语言:javascript
复制
    protected $commands = [
        'App\Console\Commands\Hello',
    ];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68887970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档