BindingResolutionException和Target不可实例化

时间:2017-05-30 16:48:46

标签: php laravel

错误:

BindingResolutionException in Container.php line 887:
Target [App\Helpers\Contracts\AccessTokenInterface] is not instantiable.

路线:

Route::get('/', 'LoginController@handleProviderCallback');

控制器:

namespace App\Http\Controllers;

use App\Helpers\Contracts\AccessTokenInterface;

class LoginController extends Controller
{

    public function handleProviderCallback(AccessTokenInterface $accessTokenInstance)
    {

        return $accessTokenInstance->getSomething();
    }
}

提供者:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Helpers\Contracts\AccessTokenInterface;
use AccessToken;

class TokenServiceProvider extends ServiceProvider
{
    protected $defer = true;

    public function boot()
    {
        //
    }

    public function register()
    {
        $this->app->bind('AccessTokenInterface::class', function(){
            return new AccessToken();
        });
    }
}

助手:

namespace App\Helpers;

use App\Helpers\Contracts\AccessTokenInterface;

class AccessToken implements AccessTokenInterface
{
    protected $something;

    public function setSomething()
     {
        $something = 100;
     }

    public function __construct()
    {
        $this->setSomething();
    }

    public function getSomething(){
        return $something;
    }

接口

namespace App\Helpers\Contracts;

Interface AccessTokenInterface
{

    public function getSomething();
    public function setSomething();
}

我已在供应商中注册提供商,并在Helper 的别名中注册 AccessToken。

我已经阅读了一些stackoverflow答案,但无法找到解决方案。我是新手。

我哪里错了?

1 个答案:

答案 0 :(得分:0)

从提供商的bind方法中删除引号:

$this->app->bind(AccessTokenInterface::class, function(){
    return new AccessToken();
});