如何编辑默认的artisan命令laravel

时间:2018-05-16 04:29:28

标签: php laravel-5 artisan

当我跑

  

php artisan make:请求“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 [
            //
        ];
    }
}

所以你可以看到上面是默认的,我想把扩展类从'FormRequest'(这是默认的)改为MyCustomFormRequest(这是我的自定义)

所以我在运行时如何实现

  

php artisan make:请求“TestRequest”

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

1 个答案:

答案 0 :(得分:1)

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

php artisan make:command CustomRequestMakeCommand

将所有代码从Illuminate \ Foundation \ Console \ RequestMakeCommand复制到App \ Console \ Commands \ CustomRequestMakeCommand(请记住也要更改类,命名空间和名称命令)

其次,在控制台文件夹名称中创建一个新的子目录,如&#34; stubs / customrequest.stub&#34; ,将所有代码从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