laravel artisan 自定义控制器模板

本文介绍了如何在laravel中自定义artisan命令来生成控制器模板。首先通过php artisan make:command创建命令类,然后在Commands目录下创建stubs目录并放入模板文件controller.plain.stub。接着修改AppMakeCommand文件,最后执行命令php artisan make:appControllers生成继承指定基类的控制器。
摘要由CSDN通过智能技术生成

laravel artisan 自定义控制器模板

1 生成命令

php artisan make:command AppMakeCommand
该命令会在 app/Console/Commands 目录下创建一个新的命令类

在 Commands 目录下 差创建 stubs 目录 放入模板文件 controller.plain.stub

controller.plain.stub 内容 :

<?php

namespace DummyNamespace;

DummyUseNamespace

class DummyClass extends DummyInheritClassController
{
   
    //
}

2 更改命令文件

AppMakeCommand 文件替换 :

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class AppMakeCommand extends Command
{
   
    protected $files;

    protected $type = 'AppControllers';

    /**
     * The name and signature of the console command. 控制台命令的名称和签名
     *
     * @var string
     */
    protected $signature = 'make:appControllers {name} {--inherit=}';

    /**
     * The console command description. 控制台命令描述
     *
     * @var string
     */
    protected $description = 'Create a new appControllers class';

    /**
     * Create a new command instance. 创建一个新的命令实例
     *
     * @return void
     */
    public function __construct(Filesystem $files)
    {
   
        parent::__construct();

        $this->files = $files;
    }

    /**
     * Execute the console command. 执行控制台命令
     * @return bool
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
     */
    public function handle()
    {
   
        $name = $this->qualifyClass($this->getNameInput());

        $path = $this->getPath($name);

        if ((! $this->hasOption('force') ||
                ! $this->option('force')) &&
            $this->alreadyExists($this->getNameInput())) {
   
            $this->error($this->type.' already exists!');

            return false;
        }

        // Next, 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值