2

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

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)

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';
    }

}

I did add also App\Providers\RepositoryServiceProvider::class, in providers section of config\app.php

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

sineverba
  • 5,059
  • 7
  • 39
  • 84

3 Answers3

5

I think @nakov might be right about it being case-sensitive. 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.

To ensure the names always match, try using the special ::class constant instead, like this:

<?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
        );
    }

}
Travis Britz
  • 5,094
  • 2
  • 20
  • 35
1

In my case i forgot to enlist the provider to confit/app.php that's why the error.

sh6210
  • 4,190
  • 1
  • 37
  • 27
0

Clear the old boostrap/cache/compiled.php:

php artisan clear-compiled

Recreate boostrap/cache/compiled.php:

php artisan optimize
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Nehad Awad
  • 39
  • 3