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 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿