dannyweeks/laravel-base-repository 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dannyweeks/laravel-base-repository

Composer 安装命令:

composer require dannyweeks/laravel-base-repository

包简介

An abstract repository class for your Eloquent repositories that requires minimal config to get started.

README 文档

README

Build Status

An abstract repository class for your Eloquent repositories that requires minimal config to get started.

Features

  • 2 minute setup.
  • 10 useful methods out of the box such as getById.
  • Flexible relationship support including eager loading..
  • Optional easy to use Caching.
  • Optional 404 exceptions when items aren't found.

Quick Start

Install via Composer.

composer require dannyweeks/laravel-base-repository

Extend your repositories with Weeks\Laravel\Repositories\BaseEloquentRepository.

Add the $model property to your repository so the base repository knows what model to use.

    namespace App\Repositories;
 
    class PostRepository extends \Weeks\Laravel\Repositories\BaseEloquentRepository
    {
        protected $model = \App\Models\Post::class;
    }

That's it! Let's test it out.

    $repo = new App\Repositories\PostRepository();
    var_dump($repo->getById(1)); // Returns the Post with an ID of 1.
    var_dump($repo->getAll()); // Returns a collection of all your posts.

Usage

Your repositories must extend the BaseEloquentRepository class and have the properties:

  • protected $model: the name of your model (including it's namespace)
  • protected $relationships: (Optional) an array of the methods available to be included when retrieving items.
    $posts = new App\Repositories\PostRepository();
    $firstPost = $posts->getById(1);
    $allPosts = $posts->getAll();
    $allPostsIncludingComments = $posts->with('comments')->getAll();

Be sure to check out the example.

Available Methods

See the repository interface class for the full API.

Relationships

Relationships are defined in the repository but are not eagerly loaded automatically.

Relationships can be loaded in the following three ways using the with() method:

  • $postRepository->with('all')->getAll(); retrieve all relationships defined in the repository class
  • $postRepository->with(['comments', 'author'])->getAll(); retrieve relationships using an array
  • $postRepository->with('comments')->getAll(); retrieve relationship using a string

An Example

This example shows how your model, repository and controller could be set up.

app\Models\Post.php

    namespace App\Models;

    class Post extends Illuminate\Database\Eloquent\Model
    {
        public function comments()
        {
            return $this->hasMany('App\Models\Comment');
        }

        public function author()
        {
            return $this->hasOne('App\Models\User');
        }
    }
    

app\Repositories\PostRepository.php

    namespace App\Repositories;
    
    use Weeks\Laravel\Repositories\BaseEloquentRepository;
    
    class PostRepository extends BaseEloquentRepository
    {
        protected $model = App\Models\Post::class;
        protected $relationships = ['comments', 'author'];
    }

app\Http\Controllers\PostController.php

    namespace App\Http\Controllers;
    
    use App\Repositories\PostRepository;
    
    class PostController extends Controller
    {
        protected $posts;
        
        public function __construct(PostRepository $posts) 
        {
            $this->posts = $posts;
        }
        
        public function show($id)
        {
            // get the post and eagerly load the comments for it too.
            $post = $this->posts->with('comments')->getById($id);
            
            return view('posts.show', compact('post'));
        }
    }

HTTP Exceptions

To enable http exceptions (like Eloquent's findOrFail method) on a repository just have it use \Weeks\Laravel\Repositories\Traits\ThrowsHttpExceptions;.

If the below methods return null they will throw a 404 error instead of returning null.

getById
getItemByColumn

An example using the ThrowsHttpExceptions trait.

    namespace App\Repositories;
    
    use Weeks\Laravel\Repositories\BaseEloquentRepository;
    use Weeks\Laravel\Repositories\Traits\ThrowsHttpExceptions;
    
    class PostRepository extends BaseEloquentRepository
    {
        use ThrowsHttpExceptions;
        
        protected $model = App\Models\Post::class;
    }

You can temporarily disable HTTP exceptions by chaining disableHttpExceptions() before performing a query. For example:

$posts = new PostRepository();

$post = $posts->disableHttpExceptions()->getById(1000); // returns null rather than throwing a 404 error.

Caching

To enable caching on a repository just have it use \Weeks\Laravel\Repositories\Traits\CacheResults;.

By doing this all the repository 'read' methods cache their results using Laravel's caching system.

// Methods that cache when using the CacheResults trait.
getAll
getPaginated
getForSelect
getById
getItemByColumn
getCollectionByColumn
getActively

An example using the CacheResults trait.

    namespace App\Repositories;
    
    use Weeks\Laravel\Repositories\BaseEloquentRepository;
    use Weeks\Laravel\Repositories\Traits\CacheResults;
    
    class PostRepository extends BaseEloquentRepository
    {
        use CacheResults;
        
        protected $model = App\Models\Post::class;
        protected $relationships = ['comments', 'author'];
        protected $nonCacheableMethods = ['getById'];
        protected $cacheTtl = 30;
    }

You can force the result of a request not to be cached by adding the method name to the $nonCacheableMethods property of your repository. See example above.

By default the ttl of a cache item is 60 minutes. This can be overwritten by updating the $cacheTtl property of your repository. See example above.

Caching can be disabled programatically by calling disabledCaching() on your repository. This method returns the repository to enable method chaining e.g. $repo->disableCaching()->getAll();.

dannyweeks/laravel-base-repository 适用场景与选型建议

dannyweeks/laravel-base-repository 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.03k 次下载、GitHub Stars 达 20, 最近一次更新时间为 2015 年 10 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「repository」 「laravel」 「repositories」 「eloquent」 「laravel 5」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 dannyweeks/laravel-base-repository 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dannyweeks/laravel-base-repository 我们能提供哪些服务?
定制开发 / 二次开发

基于 dannyweeks/laravel-base-repository 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.03k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 20
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 20
  • Watchers: 2
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-23