laravel框架自定义make:controller命令

发布时间:2020-11-17 20:42:53 阅读:1950次

在用php artisan make:controller来生成controller的时候

生成的controller文件可能不符合我们的要求,

我们有没有办法自定义controller的内容

app/Console/Commands/makeController.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;

class makeController extends GeneratorCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    //protected $signature = 'make:controller {name : 名称}';
    protected $name = 'make:controller';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    protected $type = 'Controller';  // command type

    protected function getStub() {
        return __DIR__ . '/stubs/controller.stub';
    }

    protected function getDefaultNamespace($rootNamespace) {
        return $rootNamespace . '\Http\Controllers';
    }


}
//https://blog.csdn.net/yageeart/article/details/85875470

以下是controller模板文件

app/Console/Commands/stubs/controller.stub


<?php
namespace  DummyNamespace;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Request;

class DummyClass extends Controller {

    public function index() {
        echo "<pre>";
        print_r(Request::all());
    }

}

执行命令php artisan make:controller Test/ApiController

那可生成我们自定义的controller

app/Http/Controllers/Test/ApiController.php

<?php
namespace  App\Http\Controllers\Test;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Request;

class ApiController extends Controller {

    public function index() {
        echo "<pre>";
        print_r(Request::all());
    }

}

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:laravel框架自定义make:controller命令 出自老鄢博客 | 欢迎分享