我有一个由服务提供商绑定的合同/接口,但是我得到以下错误:
RouteDependencyResolverTrait.php第81行中的ReflectionException: 类App \ Http \ Controllers \ RocketShipContract不存在
我做错了什么?
合同
namespace App\Contracts\Helpers;
Interface RocketShipContract
{
public function blastOff();
}
具体课程
namespace app\Contracts;
use App\Contracts\Helpers\RocketShipContract;
class RocketShip implements RocketShipContract
{
public function blastOff()
{
return 'Houston, we have ignition';
}
}
服务提供商
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Contracts\RocketShip;
class RocketShipServiceProvider extends ServiceProvider
{
protected $defer = true;
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind('App\Contracts\Helpers\RocketShipContract', function($app){
return new App\Contracts\RocketShip($app['HttpClient']);
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['App\Contracts\Helpers\RocketShipContract'];
}
}
控制器
public function test(RocketShipContract $rocketship)
{
$boom = $rocketship->blastOff();
return view('test.index', compact('boom'));
}
答案 0 :(得分:2)
您在问题上得到提示的错误:正在App\Http\Controllers
命名空间中解析该类。这是因为您需要在控制器中指定接口的完整命名空间。
所以要么用use
语句包含它:
use App\Contracts\Helpers\RocketShipContract;
或者键入提示完整命名空间:
public function test(App\Contracts\Helpers\RocketShipContract $rocketship)
{
// ...
}