假設:通過配置文件可以切換數據存儲驅動,比如數據存放在 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');
}
}
如何更改綁定方式,達到上述目的。謝謝。
- 1 回答
- 0 關注
- 548 瀏覽
添加回答
舉報
0/150
提交
取消