Laravel 4目标接口无法实例化 [英] Laravel 4 target interface is not instantiable

查看:134
本文介绍了Laravel 4目标接口无法实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与此问题有关如何在laravel中注册名称空间4 ,但我相信我已经解决了,并且名称空间现在可以正常工作了.

This is related to this question How to register a namespace in laravel 4 but I believe I got that worked out and namespaces are working now.

我遇到了一个新问题.我认为错误是由于尝试在控制器构造函数中键入提示而引起的,并且与使用名称空间和使用ioc有关.

There is a new problem I've run into. I believe the error is coming from trying to type hint in the controller constructor and has to do with using namespaces and using ioc.

BindingResolutionException: Target [App\Models\Interfaces\PostRepositoryInterface] is not instantiable.

在尝试引入名称空间之前,下面的方法可以完美地工作.我可以删除所有名称空间,并将接口和存储库放在同一目录中,但想知道如何使名称空间与使用ioc的这种方法一起工作.

The method below worked perfectly until I tried to introduce namespaces. I can remove all the namespaces and put the interface and repositories in the same directory but would like to know how to make namespaces work with this method of using the ioc.

以下是相关文件.

routes.php

routes.php

Route::resource('posts', 'PostsController');

PostController.php

PostController.php

<?php
use App\Models\Interfaces\PostRepositoryInterface;
class PostsController extends BaseController {

    public function __construct( PostRepositoryInterface $posts )
    {
        $this->posts = $posts; 
    }

}

PostRepositoryInterface.php

PostRepositoryInterface.php

<?php namespace App\Models\Interfaces;
interface PostRepositoryInterface {
    public function all();
    public function find($id);
    public function store($data);
}

EloquentPostRepository.php

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);
    }
}

您会看到作曲家dump-autoload确实完成了工作.

And you can see composer dump-autoload did it's job.

composer/autoload_classmap.php

composer/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',

    ....
    )

有什么想法我需要在什么地方或什么地方进行更改,以使其像使用命名空间一样工作,就像没有它们一样吗?

Any ideas where or what I need to change to make this work with namepaces like it does without them?

谢谢

推荐答案

好吧,简短的答案:在App :: bind()中使用完整的名称空间来修复第一个错误. 然后,在EloquentPostRepository.php中,因为它具有已声明的名称空间,所以它尝试将任何其他外部类调用视为在相同的名称空间中.添加一个简单的使用帖子";让PHP知道"Post"不在同一个名称空间(App \ Models \ Repositories)中.我认为这是因为一旦使用了名称空间,默认情况下其他类都将具有一个与该类名称无关的名称空间.我认为重新发布所有已纠正并正常工作的代码将是最简单的.

Ok, the short answer: Use the complete namespace in App::bind() to fix the first error. Then in EloquentPostRepository.php because it has a declared namespace it tries to treat any other external class calls as if they are in the same namespace. Adding a simple 'use Post;' lets PHP know that 'Post' is not in the same namespace (App\Models\Repositories). I assume this is because once a namespace is used, other classes by default have a namespace of whatever the class name is. I thought it would be easiest to just re-post all of the code corrected and working.

routes.php

<?php

App::bind('App\Models\Interfaces\PostRepositoryInterface',  'App\Models\Repositories\EloquentPostRepository');

Route::resource('posts', 'PostsController');

PostController.php

<?php
use App\Models\Interfaces\PostRepositoryInterface;

class PostsController extends BaseController {

    public function __construct(PostRepositoryInterface $posts)
    {
        $this->posts = $posts;
    }

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);
    }
}

PostRepositoryInterface.php

<?php namespace App\Models\Interfaces;
interface PostRepositoryInterface {
    public function all();
    public function find($id);
    public function store($data);
}

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');
    }

}

这篇关于Laravel 4目标接口无法实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆