gpt4 book ai didi

php - 拉维尔 5.7 : target is not instantiable while building

转载 作者:行者123 更新时间:2023-12-02 00:23:25 34 4
gpt4 key购买 nike

我知道有很多答案,但我无法真正解决这个问题。

我确实按照这个答案(How to make a REST API first web application in Laravel)在 Laravel 5.7 上创建了一个存储库/网关模式

我在 github 上也有“项目”,如果有人真的好心想要测试/克隆/参见:https://github.com/sineverba/domotic-panel/tree/development (开发分支)

App\Interfaces\LanInterface

<?php
/**
* Interface for LAN models operation.
*/

namespace App\Interfaces;


interface LanInterface
{

public function getAll();

}

App\Providers\ServiceProvider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
/**
* Solve the "Key too long" issue
*
* @see https://laravel-news.com/laravel-5-4-key-too-long-error
*/
Schema::defaultStringLength(191);
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->register(RepositoryServiceProvider::class);
}
}

App\Providers\RepositoryServiceProvider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class RepositoryServiceProvider extends ServiceProvider
{

public function register()
{
$this->app->bind(
'app\Interfaces\LanInterface', // Interface
'app\Repositories\LanRepository' // Eloquent
);
}

}

App\Gateways\LanGateway

<?php

/**
* The gateway talks with Repository
*/

namespace App\Gateways;
use App\Interfaces\LanInterface;


class LanGateway
{

protected $lan_interface;

public function __construct(LanInterface $lan_interface) {
$this->lan_interface = $lan_interface;
}

public function getAll()
{
return $this->lan_interface->getAll();
}

}

App\Repositories\LanRepository

<?php
/**
* Repository for LAN object.
* PRG paradigma, instead of "User"-like class Model
*/

namespace App\Repositories;
use App\Interfaces\LanInterface;
use Illuminate\Database\Eloquent\Model;


class LanRepository extends Model implements LanInterface
{

protected $table = "lans";

public function getAll()
{
return 'bla';
}

}

我还在 config\app.phpproviders 部分添加了 App\Providers\RepositoryServiceProvider::class,

这终于是 Controller 了(我知道它还不完整):

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Gateways\LanGateway;

class LanController extends Controller
{

private $lan_gateway;

/**
* Use the middleware
*
* @return void
*/
public function __construct(LanGateway $lan_gateway)
{
$this->middleware('auth');
$this->lan_gateway = $lan_gateway;
}

/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{

$this->lan_gateway->getAll();
return view('v110.pages.lan');
}
}

我得到的错误是

Target [App\Interfaces\LanInterface] is not instantiable while building [App\Http\Controllers\LanController, App\Gateways\LanGateway].

我试过:

php artisan 配置:清除php artisan 清晰编译

最佳答案

我认为@nakov 区分大小写可能是正确的。我不相信 PHP 本身关心大小写命名空间,但是 Composer 自动加载器和 Laravel 容器使用 key->value 数组键,确实有区分大小写的键,来绑定(bind)和检索类来自容器。

为确保名称始终匹配,请尝试使用特殊的 ::class相反,像这样:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Repositories\LanRepository;
use App\Interfaces\LanInterface;

class RepositoryServiceProvider extends ServiceProvider
{

public function register()
{
$this->app->bind(
LanInterface::class,
LanRepository::class
);
}

}

关于php - 拉维尔 5.7 : target is not instantiable while building,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54644273/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com