创建两个文件
App\RepositoryInterface\PhotoRepositoryInterface
App\Repository\PhotoRepository
<?php
/**
* ----------------------------------------------------------
* date: 2020/7/22 11:40
* ----------------------------------------------------------
* author: Raoxiaoya
* ----------------------------------------------------------
* describe:
* ----------------------------------------------------------
*/
namespace App\RepositoryInterface;
interface PhotoRepositoryInterface
{
}
<?php
/**
* ----------------------------------------------------------
* date: 2020/7/22 11:39
* ----------------------------------------------------------
* author: Raoxiaoya
* ----------------------------------------------------------
* describe:
* ----------------------------------------------------------
*/
namespace App\Repository;
use App\RepositoryInterface\PhotoRepositoryInterface;
class PhotoRepository implements PhotoRepositoryInterface
{
}
绑定实现关系
config/autoload/dependencies.php
添加
\App\RepositoryInterface\PhotoRepositoryInterface::class => \App\Repository\PhotoRepository::class,
在控制器使用依赖注入
<?php
declare(strict_types=1);
namespace App\Controller\Photo;
use App\Controller\AbstractController;
use App\Exception\HttpException;
use App\RepositoryInterface\PhotoRepositoryInterface;
use Hyperf\Di\Annotation\Inject;
class PhotoController extends AbstractController
{
/**
* @Inject
* @var PhotoRepositoryInterface
*/
private $photoRepository;
public function __construct()
{
}
}