我不知道為什么我會收到這個錯誤,Illuminate\Contracts\Container\BindingResolutionException: Target [App\Dal\Interfaces\IUploadsRepository] is not instantiable while building [App\Http\Controllers\FileUploadController]. in file /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 1093盡管(據我所知)一切設定正確。拼寫和其他一切都是正確的,但我仍然不確定問題是什么。
我已經在陽光下嘗試了一切來完成這項作業,但我不確定我做錯了什么。
我錯過了什么?
注意:我需要宣告這個UploadsRepository.php類,abstract因為如果我不宣告,那么我會在類名下看到一條紅色波浪線,并帶有一條警告:
Class must be declared abstract or implement methods 'resetScope', 'hidden', 'syncWithoutDetaching', 'update', 'paginate', 'delete', 'findWhereBetween', 'whereHas', 'withCount', 'find', 'getFieldsSearchable', 'create', 'findWhereNotIn', 'setPresenter', 'skipPresenter', 'all', '__callStatic', 'findWhere', 'visible', 'simplePaginate', 'firstOrNew', 'orderBy', 'sync', 'scopeQuery', 'findWhereIn', 'findByField', 'with', 'lists', 'firstOrCreate', 'updateOrCreate', '__call', 'pluck'
我不確定這是否是問題的根源,但只想提供盡可能多的資訊。
這是FileUploadController.php:
<?php
namespace App\Http\Controllers;
use App\Dal\Interfaces\IUploadsRepository;
Use App\Dal\Repositories\UploadsRepository;
class FileUploadController extends Controller
{
protected $__uploadsRepository;
public function __construct(IUploadsRepository $uploadsRepository)
{
$this->__uploadsRepository = $uploadsRepository;
}
public function getUploads(): string
{
return $this->__uploadsRepository->getUploads();
}
}
這是IUploadsRepository.php(界面):
<?php
namespace App\Dal\Interfaces;
use Prettus\Repository\Contracts\RepositoryInterface;
interface IUploadsRepository extends RepositoryInterface
{
public function getUploads();
}
這是UploadsRepository.php:
<?php
namespace App\Dal\Repositories;
use App\Dal\Interfaces\IUploadsRepository;
abstract class UploadsRepository implements IUploadsRepository
{
/**
* @return string
*/
public function getUploads(): string
{
return "test";
}
}
這是RepositoryServiceProvider.php:
<?php
namespace App\Providers;
use App\Dal\Interfaces\IUploadsRepository;
use App\Dal\Repositories\UploadsRepository;
use Illuminate\Support\ServiceProvider;
class RepositoryServiceProvider extends ServiceProvider
{
public function register() {
$this->app->bind(IUploadsRepository::class,UploadsRepository::class);
}
}
這是config/app.php:
'providers' => [
Prettus\Repository\Providers\RepositoryServiceProvider::class,
\App\Providers\RepositoryServiceProvider::class,
]
uj5u.com熱心網友回復:
您正在擴展的 RepositoryInterface 定義了您在錯誤中看到的所有方法:“'resetScope'、'hidden'、'syncWithoutDetaching'、'update'...”。您的具體實作 UploadsRepository 僅實作了 getUploads()。為了履行由 RepositoryInterface 定義的契約,您的具體實作還需要實作其他方法。我的建議是不要實作該介面,而是讓您的 UploadsRepository 從 Prettus\Repository\Eloquent\BaseRepository 類擴展,該類為這些方法提供默認實作。你可以這樣宣告。
首先 IUploadsRepository 不需要擴展 RepositoryInterface,所以宣告是:
<?php
namespace App\Dal\Interfaces;
interface IUploadsRepository
{
public function getUploads();
}
然后具體的實作是這樣的:
<?php
namespace App\Dal\Repositories;
use App\Dal\Interfaces\IUploadsRepository;
use Prettus\Repository\Eloquent\BaseRepository
class UploadsRepository extends BaseRepository implements IUploadsRepository
{
/**
* @return string
*/
public function getUploads(): string
{
return "test";
}
}
您現在擁有缺失方法的默認實作,并且可以使用 IUploadsRepository 介面通過 RepositoryServiceProvider 處理依賴項注入。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/363831.html
