isi*_*ons 6 namespaces ioc-container laravel laravel-4
这与此问题有关如何在laravel 4中注册命名空间,但我相信我已经解决了这个问题,而命名空间现在正在运行.
我遇到了一个新问题.我相信错误来自于尝试在控制器构造函数中键入提示,并且与使用命名空间和使用ioc有关.
BindingResolutionException: Target [App\Models\Interfaces\PostRepositoryInterface] is not instantiable.
Run Code Online (Sandbox Code Playgroud)
下面的方法工作得很好,直到我尝试引入命名空间.我可以删除所有名称空间并将接口和存储库放在同一目录中,但是想知道如何使用这种使用ioc的方法来使用名称空间.
这是相关文件.
routes.php文件
Route::resource('posts', 'PostsController');
Run Code Online (Sandbox Code Playgroud)
PostController.php
<?php
use App\Models\Interfaces\PostRepositoryInterface;
class PostsController extends BaseController {
public function __construct( PostRepositoryInterface $posts )
{
$this->posts = $posts;
}
}
Run Code Online (Sandbox Code Playgroud)
PostRepositoryInterface.php
<?php namespace App\Models\Interfaces;
interface PostRepositoryInterface {
public function all();
public function find($id);
public function store($data);
}
Run Code Online (Sandbox Code Playgroud)
EloquentPostRepository.php
<?php namespace App\Models\Repositories;
use App\Models\Interfaces\PostRepositoryInterface;
class EloquentPostRepository implements PostRepositoryInterface {
public function all()
{
return Post::all();
//after above edit it works to this point
//error: App\Models\Repositories\Post not found
//because Post is not in this namespace
}
public function find($id)
{
return Post::find($id);
}
public function store($data)
{
return Post::save($data);
}
}
Run Code Online (Sandbox Code Playgroud)
你可以看到composer dump-autoload做了它的工作.
作曲家/ autoload_classmap.php
return array(
'App\\Models\\Interfaces\\PostRepositoryInterface' => $baseDir . '/app/models/interfaces/PostRepositoryInterface.php',
'App\\Models\\Repositories\\EloquentPostRepository' => $baseDir . '/app/models/repositories/EloquentPostRepository.php',
....
)
Run Code Online (Sandbox Code Playgroud)
任何想法我需要改变哪些或什么来使其与名称空间一样工作没有它们?
谢谢
pat*_*shi 37
我知道这个问题已经得到了解答,但我想提醒未来的读者,这个" target interface is not instantiable
"错误的另一个常见原因是忘记注册服务提供商app/config/app.php
.
这仅适用于您扩展ServiceProvider类的情况,而不适用于您正在使用的情况App::bind()
.
我犯了太多次错误,不发表任何关于它的事情.因此,在您沿着上述冗长的路径走下去之前,请务必注册这些提供商!
isi*_*ons 13
好的,简短的回答:使用App :: bind()中的完整命名空间来修复第一个错误.然后在EloquentPostRepository.php中,因为它有一个声明的命名空间,它会尝试将任何其他外部类调用视为在同一命名空间中.添加一个简单的'使用帖子;' 让PHP知道'Post'不在同一个命名空间(App\Models\Repositories).我假设这是因为一旦使用了命名空间,默认情况下其他类具有类名所在的命名空间.我认为最简单的方法是重新发布所有纠正和工作的代码.
routes.php文件
<?php
App::bind('App\Models\Interfaces\PostRepositoryInterface', 'App\Models\Repositories\EloquentPostRepository');
Route::resource('posts', 'PostsController');
Run Code Online (Sandbox Code Playgroud)
PostController.php
<?php
use App\Models\Interfaces\PostRepositoryInterface;
class PostsController extends BaseController {
public function __construct(PostRepositoryInterface $posts)
{
$this->posts = $posts;
}
Run Code Online (Sandbox Code Playgroud)
EloquentPostRepository.php
<?php namespace App\Models\Repositories;
use App\Models\Interfaces\PostRepositoryInterface;
use Post;
class EloquentPostRepository implements PostRepositoryInterface {
public function all()
{
return Post::all();
}
public function find($id)
{
return Post::find($id);
}
public function store($data)
{
return Post::save($data);
}
}
Run Code Online (Sandbox Code Playgroud)
PostRepositoryInterface.php
<?php namespace App\Models\Interfaces;
interface PostRepositoryInterface {
public function all();
public function find($id);
public function store($data);
}
Run Code Online (Sandbox Code Playgroud)
Post.php 除了显示它没有声明的命名空间之外,这里没有任何相关内容
<?php
class Post extends BaseModel {
public static $rules = [
'title' => 'required',
'body' => 'required',
'author_id' => 'required|numeric'
];
public static $factory = [
'title' => 'string',
'body' => 'text'
];
public function user()
{
return $this->belongsTo('User', 'author_id');
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16796 次 |
最近记录: |