Controller Argument 2中的ErrorException传递给Controller :: __ construct()

时间:2016-03-01 08:20:47

标签: php laravel laravel-5 laravel-5.1

我在我的laravel应用程序中收到此错误我无法找到修复

ErrorException in FactoryController.php line 39:
Argument 2 passed to Vanguard\Http\Controllers\FactoryController::__construct() 
must implement interface Vanguard\Repositories\Factory\FactoryRepository, 
instance of Vanguard\Repositories\Country\EloquentCountry given

这些是它指向的行。

//Line 39 is below
public function __construct(CountryRepository $countries, FactoryRepository $factories, RoleRepository $roles, PermissionRepository $permissions)
{
    $this->middleware('permission:system.manage');
    $this->roles = $roles;
    $this->permissions = $permissions;
    $this->countries = $countries;
    $this->factories = $factories;

}

这是FactoriesRepositories的代码

   <?php

namespace Vanguard\Repositories\Factory;

interface FactoryRepository
{
    /**
     * Get all system factorys.
     *
     * @return mixed
     */
    public function all();

    /**
     * Finds the factory by given id.
     *
     * @param $id
     * @return mixed
     */
    public function find($id);

    /**
     * Creates new factory from provided data.
     *
     * @param array $data
     * @return mixed
     */
    public function create(array $data);

    /**
     * Updates specified factory.
     *
     * @param $id
     * @param array $data
     * @return mixed
     */
    public function update($id, array $data);

    /**
     * Remove specified factory from repository.
     *
     * @param $id
     * @return mixed
     */
    public function delete($id);
}

这是EloquentFactory

<?php

namespace Vanguard\Repositories\Factory;



use Cache;
use Vanguard\Events\Factory\Created;
use Vanguard\Events\Factory\Deleted;
use Vanguard\Events\Permission\Updated;
use Vanguard\Factories;

class EloquentFactory implements FactoryRepository
{
    /**
     * {@inheritdoc}
     */
    public function all()
    {
        return Factories::all();
    }

    /**
     * {@inheritdoc}
     */
    public function find($id)
    {
        return Factories::find($id);
    }

    /**
     * {@inheritdoc}
     */
    public function create(array $data)
    {
        $factory = Factories::create($data);

        event(new Created($factory));

        return $factory;
    }

    /**
     * {@inheritdoc}
     */
    public function update($id, array $data)
    {
        $factory = $this->find($id);

        $factory->update($data);

        Cache::flush();

        event(new Updated($factory));

        return $factory;
    }

    /**
     * {@inheritdoc}
     */
    public function delete($id)
    {
        $factory = $this->find($id);

        event(new Deleted($factory));

        $status = $factory->delete();

        Cache::flush();

        return $status;
    }
}

3 个答案:

答案 0 :(得分:1)

Laravel无法从构造函数中的类型提示FactoryRepository创建实例。您需要将具体实例绑定到接口,以便Laravel知道要实例化的内容。详情如下:

https://laravel.com/docs/5.0/container#binding-interfaces-to-implementations

答案 1 :(得分:0)

根据给定的错误,我们知道 T(n)=log(T(n-1))+1 :n>2 T(n)=O(1) :n=2 没有实现Vanguard\Repositories\Country\EloquentCountry接口。

可能的解决方法是通过扩展基础Vanguard\Repositories\Factory\FactoryRepository类或显式实现EloquentCountry接口来确保FactoryRepository类实现EloquentFactory接口。

您的FactoryRepository应如下所示:

EloquentCountry

我相信你让Laravel自己解决<?php namespace Vanguard\Repositories\Country; use Vanguard\Repositories\Factory\EloquentFactory; class EloquentCountry extends EloquentFactory { ... } 依赖关系。因此,既然您在构造函数中对FactoryController接口进行了类型提示并期望FactoryRepository实现,则需要将其绑定在容器中,如下所示:

EloquentCountry

答案 2 :(得分:0)

在注册方法中将$this->app->singleton(FactoryRepository::class, EloquentFactory::class); 添加到App\providers\AppServiceProvider.php解决了这个问题。

以下错误是我的错误

must implement interface Vanguard\Repositories\Factory\FactoryRepository, 
instance of Vanguard\Repositories\Country\EloquentCountry given

因为我错误地添加了$this->app->singleton(FactoryRepository::class, EloquentCountry ::class);