账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    laravel repository 求解?
    38
    0

    假设:通过配置文件可以切换数据存储驱动,比如数据存放在 mysql,然后修改配置文件变为 redis。
    目前我的伪代码如下:

    1:建立一个接口

    <?php
    
    namespace App\Repositories\Interfaces;
    
    Interface CategoryInterface
    {
        public function getAll();
    
        public function setData();
    }

    2:建立两个 Repository,分别对应mysql,redis数据库操作。

    <?php
    
    namespace App\Repositories\Implement;
    
    use App\Repositories\Interfaces\CategoryInterface;
    
    class CategoryMysqlRepository implements CategoryInterface
    {
        public function getAll()
        {
            // TODO: Implement getAll() method.
        }
    
        public function setData()
        {
            // TODO: Implement setData() method.
        }
    
    }
    <?php
    
    namespace App\Repositories\Implement;
    
    use App\Repositories\Interfaces\CategoryInterface;
    
    class CategoryRedisRepository implements CategoryInterface
    {
        public function getAll()
        {
            // TODO: Implement getAll() method.
        }
    
        public function setData()
        {
            // TODO: Implement setData() method.
        }
    
    }

    3:进行绑定

    <?php
    
    namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    
    class AppServiceProvider extends ServiceProvider
    {
        /**
         * Bootstrap any application services.
         *
         * @return void
         */
        public function boot()
        {
            //
        }
    
        /**
         * Register any application services.
         *
         * @return void
         */
        public function register()
        {
            $this->app->bind('App\Repositories\Interfaces\CategoryInterface',
                'App\Repositories\Implement\CategoryMysqlRepository');
        }
    }
    

    如何更改绑定方式,达到上述目的。谢谢。

    0
    打赏
    收藏
    点击回答
        全部回答
    • 共 0 条
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部