Laravel 5.7:在构建过程中,目标不可实例化。
Laravel 5.7:在构建过程中,目标不可实例化。
我知道有很多答案,但我实际上无法解决这个问题。
我按照这个答案(How to make a REST API first web application in Laravel) 在 Laravel 5.7 上创建了一个 Repository/Gateway 模式。
我也在github上有一个"项目",如果有人真的想测试/克隆/查看: https://github.com/sineverba/domotic-panel/tree/development (开发分支)
App\Interfaces\LanInterface
App\Providers\ServiceProvider
app->register(RepositoryServiceProvider::class); } }
App\Providers\RepositoryServiceProvider
app->bind( 'app\Interfaces\LanInterface', // 接口 'app\Repositories\LanRepository' // Eloquent ); } }
App\Gateways\LanGateway
lan_interface = $lan_interface; } public function getAll() { return $this->lan_interface->getAll(); } }
App\Repositories\LanRepository
我还在
config\app.php
的providers
部分中添加了App\Providers\RepositoryServiceProvider::class,
这是最终的控制器(我知道它不完整):
middleware('auth'); $this->lan_gateway = $lan_gateway; } /** * 显示资源列表。 * * @return \Illuminate\Contracts\Support\Renderable */ public function index() { $this->lan_gateway->getAll(); return view('v110.pages.lan'); } }
我得到的错误是
在构建 [App\Http\Controllers\LanController, App\Gateways\LanGateway] 时,目标 [App\Interfaces\LanInterface] 无法实例化。
我尝试了:
php artisan config:clear
php artisan clear-compiled
Laravel 5.7版本中出现了一个问题,错误提示为“target is not instantiable while building”。下面将介绍这个问题的出现原因以及解决方法。
该问题的解决方法如下:
1. 清除旧的编译缓存文件boostrap/cache/compiled.php
,命令如下:
php artisan clear-compiled
2. 重新生成编译缓存文件boostrap/cache/compiled.php
,命令如下:
php artisan optimize
通过执行上述两个命令,可以解决“target is not instantiable while building”错误。这是因为在Laravel 5.7版本中,编译缓存文件compiled.php
可能会出现问题,而执行上述命令可以清除旧缓存文件,并重新生成新的缓存文件,从而解决了该问题。
以上就是解决Laravel 5.7版本中出现“target is not instantiable while building”错误的方法。希望对大家有所帮助。
在Laravel 5.7版本中,可能会遇到"target is not instantiable while building"的问题。该问题的出现是因为Laravel容器和Composer自动加载器使用了键值对数组键来绑定和检索容器中的类,而这些键是区分大小写的。
为了确保名称始终匹配,可以尝试使用特殊的`::class`常量来代替字符串形式的类名。具体操作如下:
app->bind( LanInterface::class, LanRepository::class ); } }
通过使用`::class`常量,可以确保类的命名空间和大小写一致,从而解决了"target is not instantiable while building"的问题。
感谢您的阅读!