安利

l5-repository 库用于laravel抽象数据层,使我们的应用程序的维护更加灵活。为了保持代码的整洁性和可读性,使用 l5-repository 是非常有必要的,虽然我们大可不必使用这种模式去使用 Laravel 。Repository 模式使我们的 Controller 不在那么啰嗦、解耦和易读,当然也很容易排错。

Repository 模式的原理就是数据层和逻辑层进行分离,Repository 抽象数据层封装了大部分常用的增删改查操作,不需要我们重复的去写代码(后面介绍其他特性)。 Controller 只负责从 Repository 数据层中获取数据然后进行接下来的逻辑 - 条件判断、数据处理等等。开发中代码出错时,我们能很快定位到是数据层(Repository)出错还是逻辑层(Controller),更好的帮助我们处理问题。

安装

上面已经安利完了,接下来我们从安装开始使用 l5-repository 库。完全从新安装,所以我们先新建一个项目:

laravel new laravel-repository

目前最新版本是Laravel5.4,其他版本使用本扩展大同小异,只需要注意安装的扩展版本就行了

Composer

执行以下命令获取最新版本的包:

composer require prettus/l5-repository

l5-repository packagist地址:https://packagist.org/packages/prettus/l5-repository
l5-repository github地址:https://github.com/andersao/l5-repository

Laravel

config/app.php 中添加 providers :

'providers' => [
    ...
    Prettus\Repository\Providers\RepositoryServiceProvider::class,
],

生成配置文件:

php artisan vendor:publish --provider "Prettus\Repository\Providers\RepositoryServiceProvider"

生成的配置文件在 config/repository.php,下面是配置文件的详细信息:

<?php
/*
|--------------------------------------------------------------------------
| Prettus Repository Config
|--------------------------------------------------------------------------
|
|
*/
return [

    /*
    |--------------------------------------------------------------------------
    | 默认分页每页显示数量
    |--------------------------------------------------------------------------
    |
    */
    'pagination' => [
        'limit' => 15
    ],

    /*
    |--------------------------------------------------------------------------
    | Fractal Presenter Config
    |--------------------------------------------------------------------------
    |
    序列化相关
    Available serializers:
    ArraySerializer
    DataArraySerializer
    JsonApiSerializer

    */
    'fractal'    => [
        'params'     => [
            'include' => 'include'
        ],
        'serializer' => League\Fractal\Serializer\DataArraySerializer::class
    ],

    /*
    |--------------------------------------------------------------------------
    | Cache Config 查询缓存
    |--------------------------------------------------------------------------
    |
    */
    'cache'      => [
        /*
         |--------------------------------------------------------------------------
         | Cache Status
         |--------------------------------------------------------------------------
         |
         | Enable or disable cache
         |
         */
        'enabled'    => true,

        /*
         |--------------------------------------------------------------------------
         | Cache Minutes
         |--------------------------------------------------------------------------
         |
         | Time of expiration cache
         |
         */
        'minutes'    => 30,

        /*
         |--------------------------------------------------------------------------
         | Cache Repository
         |--------------------------------------------------------------------------
         |
         | Instance of Illuminate\Contracts\Cache\Repository
         |
         */
        'repository' => 'cache',

        /*
          |--------------------------------------------------------------------------
          | Cache Clean Listener 清楚查询缓存
          |--------------------------------------------------------------------------
          |
          |
          |
          */
        'clean'      => [

            /*
              |--------------------------------------------------------------------------
              | Enable clear cache on repository changes
              |--------------------------------------------------------------------------
              |
              */
            'enabled' => true,

            /*
              |--------------------------------------------------------------------------
              | Actions in Repository
              |--------------------------------------------------------------------------
              |
              | create : Clear Cache on create Entry in repository
              | update : Clear Cache on update Entry in repository
              | delete : Clear Cache on delete Entry in repository
              |
              */
            'on'      => [
                'create' => true,
                'update' => true,
                'delete' => true,
            ]
        ],

        'params'     => [
            /*
            |--------------------------------------------------------------------------
            | Skip Cache Params 搜索查询跳过缓存时参数名称
            |--------------------------------------------------------------------------
            |
            |
            | Ex: http://prettus.local/?search=lorem&skipCache=true
            |
            */
            'skipCache' => 'skipCache'
        ],

        /*
       |--------------------------------------------------------------------------
       | Methods Allowed 指定那些方法能够使用或者排除一些方法不允许使用,不常用
       |--------------------------------------------------------------------------
       |
       | methods cacheable : all, paginate, find, findByField, findWhere, getByCriteria
       |
       | Ex:
       |
       | 'only'  =>['all','paginate'],
       |
       | or
       |
       | 'except'  =>['find'],
       */
        'allowed'    => [
            'only'   => null,
            'except' => null
        ]
    ],

    /*
    |--------------------------------------------------------------------------
    | Criteria Config Criteria查询条件
    |--------------------------------------------------------------------------
    |
    | Settings of request parameters names that will be used by Criteria
    |
    */
    'criteria'   => [
        /*
        |--------------------------------------------------------------------------
        | Accepted Conditions
        |--------------------------------------------------------------------------
        |
        | Conditions accepted in consultations where the Criteria
        |
        | Ex:
        |
        | 'acceptedConditions'=>['=','like']
        |
        | $query->where('foo','=','bar')
        | $query->where('foo','like','bar')
        |
        */
        'acceptedConditions' => [
            '=',
            'like'
        ],
        /*
        |--------------------------------------------------------------------------
        | Request Params 查询键定义
        |--------------------------------------------------------------------------
        |
        | Request parameters that will be used to filter the query in the repository
        |
        | Params :
        |
        | - search : Searched value
        |   Ex: http://prettus.local/?search=lorem
        |
        | - searchFields : Fields in which research should be carried out
        |   Ex:
        |    http://prettus.local/?search=lorem&searchFields=name;email
        |    http://prettus.local/?search=lorem&searchFields=name:like;email
        |    http://prettus.local/?search=lorem&searchFields=name:like
        |
        | - filter : Fields that must be returned to the response object
        |   Ex:
        |   http://prettus.local/?search=lorem&filter=id,name
        |
        | - orderBy : Order By
        |   Ex:
        |   http://prettus.local/?search=lorem&orderBy=id
        |
        | - sortedBy : Sort
        |   Ex:
        |   http://prettus.local/?search=lorem&orderBy=id&sortedBy=asc
        |   http://prettus.local/?search=lorem&orderBy=id&sortedBy=desc
        |
        */
        'params'             => [
            'search'       => 'search',
            'searchFields' => 'searchFields',
            'filter'       => 'filter',
            'orderBy'      => 'orderBy',
            'sortedBy'     => 'sortedBy',
            'with'         => 'with'
        ]
    ],
    /*
    |--------------------------------------------------------------------------
    | Generator Config generator生成器生成对应文件所在目录,常用,一般都会自定义
    |--------------------------------------------------------------------------
    |
    */
    'generator'  => [
        'basePath'      => app_path(),
        'rootNamespace' => 'App\\',
        'paths'         => [
            'models'       => 'Entities',
            'repositories' => 'Repositories',
            'interfaces'   => 'Repositories',
            'transformers' => 'Transformers',
            'presenters'   => 'Presenters',
            'validators'   => 'Validators',
            'controllers'  => 'Http/Controllers',
            'provider'     => 'RepositoryServiceProvider',
            'criteria'     => 'Criteria',
            'stubsOverridePath' => app_path()
        ]
    ]
];

OK,安装篇就到这里结束了,代码地址:https://github.com/lanceWan/laravel-repository 。本文章代码在github对应的 v1.0 分支上。


Source: http://blog.iwanli.me/article/wr9Em9mL.html

Leave a comment