簡體   English   中英

如何將句柄方法調用到 routes.php 文件中?

[英]How to call a handle method into an routes.php file?

我想在我的命令行中運行php artisan make:greetings命令時打印一條日志消息,它應該在我的日志文件中返回一條消息,關於如何調用 routes.php 文件中的 handle 方法,為此我正在寫一些代碼,但我收到以下錯誤請幫我解決這個問題

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");
    }
}

第二個參數必須是一個閉包,你正在傳遞一個數組,這個閉包用於將數據傳遞給你的句柄方法,因為你的句柄方法中沒有任何 arguments 使用調用方法而不是命令,試試這個:

Artisan::call('make:greetings');

並且不要忘記在 App\Console\Kernel class 中注冊您的命令:

    protected $commands = [
        'App\Console\Commands\Hello',
    ];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM