【开发记录】创建新的 PHP artisan 命令

目录


创建新的 PHP artisan 命令

本分类文章的任何解决方案只是针对个人项目的尝试
GIT https://github.com/cuiliwu/Laravel-B2C-Shopping-system

为方便开发,创建新的 php artisan make:repository table_name 命令,可以同时创建model interface 及 repository


查看已有命令的列表

php artisan list

以下是部分命令列表
截取部分命令

创建命令实现文件

无make:console 命令来自动创建Commands 下的PHP文件,就手动创建了一个。
部分实现需求功能代码


    /**
     * Execute the command.
     *
     * @return void
     */
    public function fire()
    {
        $this->call('make:repository', [
            'name' => $this->name
        ]);
        try {
            // Add entity repository binding to the repository service provider
            $provider = File::get($this->getPath());
            $repositoryInterface = '\\' . $this->getRepository() . "::class";
            $repositoryEloquent = '\\' . $this->getEloquentRepository() . "::class";
            File::put($this->getPath(), str_replace($this->bindPlaceholder, "\$this->app->bind({$repositoryInterface}, $repositoryEloquent);" . PHP_EOL . '        ' . $this->bindPlaceholder, $provider));
        } catch (FileAlreadyExistsException $e) {
            $this->error('创建失败');
        }
    }

发现缺少组件 prettus/l5-repository ,执行命令

composer require prettus/l5-repository

此时重新查看 命令列表 php artisan list
已增加新的命令

编写服务提供者

app\Providers\RepositoryServiceProvider.php注册仓库

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class RepositoryServiceProvider extends ServiceProvider
{


    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->commands('\App\Console\Commands\BindingsCommand');  

        //:end-bindings:
    }

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }


}

配置文件设置


    'generator'  => [
        'basePath'      => app()->path() . '/Repository',
        'rootNamespace' => 'App\\Repository\\',
        'stubsOverridePath' => app()->path() . '/Console',
        'paths'         => [
            'models'       => 'Models',
            'repositories' => 'Repositories',
            'interfaces'   => 'Repositories/Interfaces',
            'transformers' => 'Transformers',
            'presenters'   => 'Presenters',
            'validators'   => 'Validators',
            'controllers'  => 'Http/Controllers',
            'provider'     => 'RepositoryServiceProvider',
            'criteria'     => 'Criteria'
        ]
    ]

命令尝试执行

php artisan make:repository admin_user
命令执行

执行结果
执行结果

完成。


猜你喜欢

转载自blog.csdn.net/cuiliwu/article/details/80434874
今日推荐