如何编辑默认 artisan 命令 laravel

how to edit default artisan command laravel

当我运行

php artisan make:request "TestRequest"

它将创建如下文件:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class TestRequest extends FormRequest // i want to change from extends 'Form Request' to extends 'MyCustomFormRequest'
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }
}

所以你在上面看到的是默认的,我想将 extends class 从 'FormRequest'(这是默认的)更改为 MyCustomFormRequest(这是我的自定义)

那么当我 运行

我该如何实现

php artisan make:request "TestRequest"

,它会自动扩展 'MyCustomFormRequest' 而不是 'FormRequest' ?

首先,您需要创建一个新命令

php artisan make:command CustomRequestMakeCommand

将所有代码从 Illuminate\Foundation\Console\RequestMakeCommand 复制到 App\Console\Commands\CustomRequestMakeCommand(记得还要更改 class、命名空间和名称命令)

其次,在控制台文件夹名称中创建一个新子,如 "stubs/customrequest.stub",将所有代码从 request.stub (vendor/laravel/framework/src/Illuminate/Foundation/Console/stubs/request.stub) 复制到新的,将 FromRequest 更改为 YourCustomFormRequest

class DummyClass extends CustomFormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }
}

然后您可以使用您的自定义命令,您可以在以下位置阅读更多相关信息 https://laravel.com/docs/5.6/artisan