POV
POV

Reputation: 12015

Target [App\Service\InvitationServiceInterface] is not instantiable?

I have service provider:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class InvitationServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('App\Service\InvitationServiceInterface', 'App\Service\InvitationService');
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

Also there is an custom interface InvitationServiceInterface and InvitationService:

<?php
namespace App\Service;

class InvitationService implements InvitationServiceInterface
{
    public function doAwesomeThing()
    {
        echo 'Do...';
    }
}

Interface is:

<?php
namespace App\Service;

interface InvitationServiceInterface
{
    public function doAwesomeThing();
}

These both files are place in path:

App\Service\InvitationServiceInterface
App\Service\InvitationService

So, I get an error:

Illuminate \ Contracts \ Container \ BindingResolutionException Target [App\Service\InvitationServiceInterface] is not instantiable.

Using is:

use App\Service\InvitationServiceInterface;
use App\User;
use Illuminate\Http\Request;

class PassportController extends Controller
{

    public function register(Request $request, InvitationServiceInterface $invitationService)
    {
}

Upvotes: 1

Views: 1625

Answers (2)

112Legion
112Legion

Reputation: 1237

Probably service provider was cached.

Try to delete those two files:

enter image description here

Or run php artisan config:clear

Or delete them derectry

rm bootstrap/cache/packages.php
rm bootstrap/cache/services.php

Upvotes: 0

dparoli
dparoli

Reputation: 9161

You can use the laravel service container in the constructor of a controller, i.e.:

class PassportController extends Controller
{
    public function _construct(InvitationServiceInterface $invitationService)
    {
        $this->invitationService = $invitationService;
    }
}

But not in a route controller function like you tried because here is the area of the route model binding so the service container is trying to instanciate a route model.

Upvotes: 3

Related Questions