Php Laravel 5.7:目标在构建时不可实例化
我知道有很多答案,但我不能真正解决这个问题 我确实按照这个答案()在Laravel5.7上创建了一个存储库/网关模式 我在github上也有“项目”,如果有人真的想要测试/克隆/查看:(开发分支) App\Interfaces\LanInterfacePhp Laravel 5.7:目标在构建时不可实例化,php,laravel,Php,Laravel,我知道有很多答案,但我不能真正解决这个问题 我确实按照这个答案()在Laravel5.7上创建了一个存储库/网关模式 我在github上也有“项目”,如果有人真的想要测试/克隆/查看:(开发分支) App\Interfaces\LanInterface <?php /** * Interface for LAN models operation. */ namespace App\Interfaces; interface LanInterface { public f
<?php
/**
* Interface for LAN models operation.
*/
namespace App\Interfaces;
interface LanInterface
{
public function getAll();
}
我得到的错误是
Target [App\Interfaces\LanInterface] is not instantiable while building [App\Http\Controllers\LanController, App\Gateways\LanGateway].
我确实试过:
php artisan配置:清除
php artisan clear compiled
我认为@nakov关于区分大小写的说法可能是正确的。我认为PHP本身并不关心大小写名称空间,但是composer autoloader和Laravel容器使用key->value数组键来绑定和检索容器中的类,而key->value数组键具有区分大小写的键
要确保名称始终匹配,请尝试使用特殊常量,如下所示:
在我的例子中,我忘了将提供者登记到confit/app.php这就是错误的原因。这有帮助吗?@BILALMALIK我确实在app\Providers\RepositoryServiceProvider中绑定了我不确定,但是你能将你的书写约定从PSR-0更改为PSR-4,或者反之亦然吗?@Sinerba我认为名称空间是case敏感,因此当将接口绑定到实现而不是app\..
tryapp\..
@nakov yes时,这就是问题所在。。。谢谢:)明白问题了:D非常感谢!
<?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();
}
}
<?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';
}
}
<?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].