【问题标题】:Laravel 5.7: target is not instantiable while buildingLaravel 5.7:构建时目标不可实例化
【发布时间】:2019-07-05 18:10:00
【问题描述】:

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

我确实按照这个答案 (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
        );
    }

}

应用\网关\局域网网关

<?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,

这终于是控制器了(我知道不完整):

<?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 config:clear php artisan clear-compiled

【问题讨论】:

  • @BILALMALIK 我确实绑定了App\Providers\RepositoryServiceProvider
  • 我不确定,但您能否将您的书写约定从 PSR-0 更改为 PSR-4,反之亦然?
  • @sineverba 我认为命名空间是区分大小写的,所以在将接口绑定到实现而不是app\.. 时尝试App\..
  • @nakov 是的,这就是问题所在...谢谢 :)

标签: php laravel


【解决方案1】:

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

为确保名称始终匹配,请尝试改用特殊的 ::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
        );
    }

}

【讨论】:

  • 得到问题 :D 非常感谢!
【解决方案2】:

在我的情况下,我忘记将提供程序加入 confit/app.php 这就是错误的原因。

【讨论】:

    【解决方案3】:

    清除旧的boostrap/cache/compiled.php

    php artisan clear-compiled
    

    重新创建boostrap/cache/compiled.php:

    php artisan optimize
    

    【讨论】:

      猜你喜欢
      • 2018-11-28
      • 2015-06-07
      • 2022-10-16
      • 2016-06-22
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-05
      相关资源
      最近更新 更多