Search code examples
phplaravel-5laravel-artisan

how to edit default artisan command laravel


when I run

php artisan make:request "TestRequest"

it will create file like below :

<?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 [
            //
        ];
    }
}

so as you can see above is the default one, I wanted to change extends class from 'FormRequest' (this is default) to MyCustomFormRequest (this is my custom)

so how do I achieve when I run

php artisan make:request "TestRequest"

, it will automatically extends 'MyCustomFormRequest' instead of 'FormRequest' ?


Solution

  • First ,you need to create a new command

    php artisan make:command CustomRequestMakeCommand
    

    Copy all code from Illuminate\Foundation\Console\RequestMakeCommand to App\Console\Commands\CustomRequestMakeCommand ( Remember to change the class ,namespace,and name command also)

    Secondly ,create a new sub at console folder name like "stubs/customrequest.stub" ,copy all code from request.stub (vendor/laravel/framework/src/Illuminate/Foundation/Console/stubs/request.stub) to the new one ,change the FromRequest to 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 [
                //
            ];
        }
    }
    

    Then you can use your custom command ,you can read more about this at https://laravel.com/docs/5.6/artisan