[英]Laravel 5.7: target is not instantiable while building
I know there are so many answer, but I cannot really solve this.我知道有很多答案,但我无法真正解决这个问题。
I did follow this answer ( How to make a REST API first web application in Laravel ) to create a Repository/Gateway Pattern on Laravel 5.7我确实按照这个答案( 如何在 Laravel 中制作 REST API 第一个 Web 应用程序)在 Laravel 5.7 上创建存储库/网关模式
I have also the "project" on github, if someone really kindly want test/clone/see : https://github.com/sineverba/domotic-panel/tree/development (development branch)我在 github 上也有“项目”,如果有人真的想要 test/clone/see : https : //github.com/sineverba/domotic-panel/tree/development (开发分支)
App\\Interfaces\\LanInterface 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 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应用\\网关\\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 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';
}
}
I did add also App\\Providers\\RepositoryServiceProvider::class,
in providers
section of config\\app.php
我也添加了
App\\Providers\\RepositoryServiceProvider::class,
在config\\app.php
providers
部分
This is finally the controller (I know that it is not complete):这是最后的控制器(我知道它不完整):
<?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');
}
}
And the error that I get is我得到的错误是
Target [App\Interfaces\LanInterface] is not instantiable while building [App\Http\Controllers\LanController, App\Gateways\LanGateway].
I did try:我确实尝试过:
php artisan config:clear
php artisan clear-compiled
php artisan config:clear
php artisan clear-compiled
I think @nakov might be right about it being case-sensitive.我认为@nakov 区分大小写可能是正确的。 I don't believe PHP itself cares about upper/lowercase namespaces, but the composer autoloader and the Laravel container use key->value array keys, which do have case-sensitive keys, to bind and retrieve classes from the container.
我不相信 PHP 本身关心大写/小写命名空间,但 composer 自动加载器和 Laravel 容器使用 key->value 数组键,它们确实有区分大小写的键,从容器中绑定和检索类。
To ensure the names always match, try using the special ::class
constant instead, like this:为确保名称始终匹配,请尝试使用特殊的
::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
);
}
}
在我的情况下,我忘记将提供者招募到confit/app.php这就是错误的原因。
php artisan clear-compiled php artisan 清晰编译
php artisan optimize php工匠优化
I think these commands will help you out.我认为这些命令会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.