dongyang0005 2015-11-20 16:26
浏览 35
已采纳

Laravel - 定义存储库覆盖

I am using Laravel 5.1 and have set up a Repository pattern. I have the concrete implementations of my repos injected into my controllers. I realize that your SUPPOSED to inject the interface but that over complicates my API and doesn't solve my issue. I have a client config that simply contains a string such as '' and I am already using that globally to use model overrides if they exist.

So, for example, if I have client 'yahoo' and they have an override in my Overrides/Yahoo/Models/User.php then it will use this User.php. Whether its an extension of the base User model or a whole new implementation is up to me.

I am trying to do the same thing for my repositories. I want to be able to put an override file in Overrides/Yahoo/Repos/UserRepository.php and on injection it will either use the base User repository or the override if it exists.

Any ideas of how I can accomplish this? I realize that you can inject a repository interface and use a binding but I want to do this globally. If you can tell me how I can abstract that functionality to automatically bind the correct implementation of the interface based on client config I would accept that.

  • 写回答

1条回答 默认 最新

  • douxia6163 2015-11-20 17:12
    关注

    You can resolve your concrete implementation by the configuration you use, but it requires that you use interfaces.

    Example configuration (config/client.php):

    <?php
    
    return [
    
        'users' => [
            'repository' => \App\Overrides\Yahoo\Repos\UserRepository::class,
        ],
    
    ];
    

    In your AppServiceProviders register method:

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(
            UserRepositoryInterface::class,
            config('client.users.repository')
        );
    }
    

    In your Yahoo UserRepository, you can then inject the Yahoo User model directly:

    <?php
    
    namespace App\Overrides\Yahoo\Repos;
    
    use App\Repos\UserRepositoryInterface;
    use App\Overrides\Yahoo\Models\User;
    
    class UserRepository implements UserRepositoryInterface
    {
    
        private $user;
    
        public function __construct(User $user)
        {
            $this->user = $user;
        }
    
        // TODO: Implement UserRepositoryInterface methods
    
    }
    

    Finally, in your UserController you can inject the UserRepositoryInterface and it will bind the concrete implementation based on your configuration:

    <?php
    
    namespace App\Http\Controllers;
    
    use App\Repos\UserRepositoryInterface;
    
    class UserController extends Controller
    {
    
        private $users;
    
        public function __construct(UserRepositoryInterface $users)
        {
            $this->users = $users;
        }
    
    }
    

    It might seem overkill at first, but once you set up everything it's pretty easy to add new overrides.

    You could even create a base concrete implementation of the UserRepository and make every override repository inherit from it. This way you don't have to re-implement all methods required by the interface, but you stay flexible when the user repositories use different database technologies (SQL, MondoDB...)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀