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

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

问题描述

这与这个问题有关 How to register a namespace in laravel4 但我相信我已经解决了,并且命名空间现在正在工作.

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 [AppModelsInterfacesPostRepositoryInterface] 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

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

PostController.php

PostController.php

<?php
use AppModelsInterfacesPostRepositoryInterface;
class PostsController extends BaseController {

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

}

PostRepositoryInterface.php

PostRepositoryInterface.php

<?php namespace AppModelsInterfaces;
interface PostRepositoryInterface {
    public function all();
    public function find($id);
    public function store($data);
}

EloquentPostRepository.php

EloquentPostRepository.php

<?php namespace AppModelsRepositories;
use AppModelsInterfacesPostRepositoryInterface;
class EloquentPostRepository implements PostRepositoryInterface {

    public function all()
    {
        return Post::all();
            //after above edit it works to this point
            //error: AppModelsRepositoriesPost 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);
    }
}

你可以看到 composer dump-autoload 完成了它的工作.

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

作曲家/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"不在同一个命名空间(AppModelsRepositories)中.我认为这是因为一旦使用了命名空间,其他类默认具有任何类名的命名空间.我认为重新发布所有已更正并正常工作的代码是最简单的.

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 (AppModelsRepositories). 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('AppModelsInterfacesPostRepositoryInterface',  'AppModelsRepositoriesEloquentPostRepository');

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

PostController.php

<?php
use AppModelsInterfacesPostRepositoryInterface;

class PostsController extends BaseController {

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

EloquentPostRepository.php

<?php namespace AppModelsRepositories;
use AppModelsInterfacesPostRepositoryInterface;
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 AppModelsInterfaces;
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天全站免登陆