think-repositories is a package for Thinkphp 6.0 which is used to abstract the database layer. This makes applications much easier to maintain.
Run the following command from you terminal:
composer require think/repositories
First, create your repository class. Note that your repository class MUST extend think\Repository
and implement model() method
<?php
namespace app\repositories;
use think\Repository;
class FilmsRepository extends Repository {
public function model() {
return 'App\Film';
}
}
It can be created using the make:R
command:
php think make:R admin@data/City common@data/DataCity --E=common@BaseRepository
By implementing model()
method you telling repository what model class you want to use. Now, create App\Film
model:
<?php
namespace app\model;
use think\Model;
class Film extends Model {
protected $primaryKey = 'film_id';
protected $table = 'film';
protected $casts = [
"rental_rate" => 'float'
];
}
And finally, use the repository in the controller:
<?php
namespace app\controller;
use app\repositories\FilmsRepository as Film;
class FilmsController extends Controller {
private $film;
public function __construct(Film $film) {
$this->film = $film;
}
public function index() {
return \Response::json($this->film->all());
}
}
This package is largely inspired by this great package by @bosnadev.