我正在使用设计模式存储库,我写的每件事都是正确的,但我有一个错误返回目标[AppRepositorycategoryRepositoryInterface]在构建[AppHttpControllersCategoryController]时不可实例化。我不知道为什么,虽然我写的每件事都是正确的,请帮助
这是我的代码
我的配置/应用
AppProvidersRepositoryServiceProvider::class,
和我的categorycontroller
<?php
namespace AppHttpControllers;
use AppRepositorycategoryRepositoryInterface;
use IlluminateHttpRequest;
class CategoryController extends Controller
{
private categoryRepositoryInterface $categoryRepository;
public function __construct(categoryRepositoryInterface $categoryRepository)
{
$this->categoryRepository = $categoryRepository;
}
and my repositoryinterface
<?php
namespace AppRepository;
use IlluminateHttpRequest;
interface categoryRepositoryInterface
{
public function createCategory(Request $request);
public function validation(Request $request);
}
和我的类别存储库
<?php
namespace AppRepository;
use AppModelsCategory;
use IlluminateHttpRequest;
use IlluminateSupportFacadesValidator;
class categoryRepository implements categoryRepositoryInterface{
public function validation(Request $request){
$validation = Validator::make($request->all(),[
'name' => 'required',
]);
if($validation->fails()){
return response()->json([
'status' => 400,
'errors' => $validation->errors(),
]);
}
}
public function createCategory(Request $request)
{
$category = new Category();
$category->name = $request->name;
$category->save();
}
}
和my servicerepositoryprovider
<?php
namespace AppProviders;
use AppRepositoryCategoryRepository;
use AppRepositoryCategoryRepositoryInterface;
use IlluminateSupportServiceProvider;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->bind(CategoryRepositoryInterface::class, CategoryRepository::class);
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
您的RepositoryServiceProvier
有错别字
use AppRepositorycategoryRepository; //First alphabet should be lower case
use AppRepositorycategoryRepositoryInterface; //First alphabet should be lower case
use IlluminateSupportServiceProvider;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//First alphabet should be lower case as per your class and interface names
$this->app->bind(categoryRepositoryInterface::class, categoryRepository::class);
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
我想给你一个建议。当使用Laravel进行开发时,遵循Laravel的命名约定会更容易。以下约定也将帮助你更好地理解互联网上的例子。
无论你做了什么决定,记住要坚持下去。在这个特殊的例子中,你用驼峰大小写命名类以小写字母开头所以当通过导入时使用你需要坚持的语句