我知道这个问题被问了很多次,但没有一个答案对我有帮助.
我在Laravel 5中遇到了异常
BindingResolutionException in Container.php line 785:
Target [App\Contracts\CustomModelInterface] is not instantiable.
Run Code Online (Sandbox Code Playgroud)
我没做过什么就做了:
App\Providers\AppRepositoryProvider的app.php供应商php artisan clear-compiled结构体:
app
- Contracts
- CustomModelInterface.php
- Models
- CustomModel.php
- Repositories
- CustomModelRepository.php
- Providers
- AppRepositoryProvider.php
- Services
- MyService.php
Run Code Online (Sandbox Code Playgroud)
应用程序\合同\ CustomModelInterface.php
<?php namespace App\Contracts;
interface CustomModelInterface {
public function get();
}
Run Code Online (Sandbox Code Playgroud)
应用程序\库\ CustomModelRepository.php
<?php namespace App\Repositories;
use App\Contracts\CustomModelInterface;
use App\Models\CustomModel;
class CustomModelRepository implements CustomModelInterface {
private $Model;
public function __construct(CustomModel $model) {
$this->Model = …Run Code Online (Sandbox Code Playgroud) 我用laravelUploader包形成这个链接来上传我的文件.当我使用这个包在内腔发送文件时,代码如下:
$file = $this->uploader->file($request->file('file'))->push(storage_path('app'));
return $this->respondCreated(['data'=>$file->getFullPath()]);
Run Code Online (Sandbox Code Playgroud)
我得到一个错误,如:
BindingResolutionException in Container.php line 752:
Target [Illuminate\Contracts\Filesystem\Factory] is not instantiable while building [Almazik\LaravelUploader\LaravelUploader].
Run Code Online (Sandbox Code Playgroud)
现在我该怎么办呢?
所以我有一个BaseValuation抽象类及其实现示例FooValuation和BarValuation.
我想根据使用输入来实例化Foo或实现。Bar所以我自己创建了一个简单的类,Valuation它的作用是:
<?php
namespace App\Valuation;
class Valuation
{
public $class;
public function __construct($id, $type = 'basic')
{
$class = 'App\Valuation\Models\\' . ucfirst($type) . 'Valuation';
$this->class = new $class($id);
}
public function make()
{
return $this->class;
}
}
Run Code Online (Sandbox Code Playgroud)
使用这个我可以简单地做到这一点(new App\Valuation\Valuation($id, $type))->make(),我将根据用户的要求获得所需的实现。
但我知道 laravel 的容器很强大,必须允许我以某种方式做到这一点,但我不明白这是如何完成的。有人有什么想法吗?