Laravel DI问题

时间:2018-01-11 17:11:53

标签: php laravel dependency-injection

我已经创建了一个app.php并使用它应该拥有的方法实现了接口。之后,我在我的服务提供商中注册了它,该服务提供商也在class UserRepository extends AbstractUserRepository implements UserRepositoryInterface { public function __construct(User $user = null) { $this->model = $user ?: $this->getAuthenticatedUser(); } ... } 注册。

UserRepository

AbstractUserRepository扩展EloquentRepository扩展EloquentRepository,它应该抽象出一些通用的雄辩动作。

model有受保护的成员getModel()及其获取者Route::get('test', function () { $repo = new \Account\Repository\UserRepository(); return $repo->getModel(); });

现在我遇到的问题是初始化似乎不能正常工作。登录并从测试路径转储后:

use Account\Contracts\UserRepositoryInterface as UserRepository;

private $userRepository;

public function __construct(UserRepository $userRepository)
{
    $this->userRepository = $userRepository;
}

我正在找回经过身份验证的用户。

但是,如果我使用DI做同样的事情,我会收到错误:

return $this->userRepository->getModel();

然后在控制器方法中执行转储

User

我收到了错误消息。

具体来说,似乎它正在尝试初始化 $this->app->bind('User', User::class); $this->app->bind('UserRepository', UserRepository::class); $this->app->bind(UserRepositoryInterface::class, UserRepository::class); $this->app->bind('AbstractUserRepository', AbstractUserRepository::class); $this->app->bind(AbstractUserRepositoryInterface::class, AbstractUserRepository::class); public function provides() { return [ 'User', 'UserRepository', UserRepositoryInterface::class, AbstractUserRepositoryInterface::class, 'AbstractUserRepository', 模型,但由于用户未实际转发而失败

编辑所有绑定:

public static List<CityEvent> getData() {
        List<CityEvent> list = new ArrayList<>();
        list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
        list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
        list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
        list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
        list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
        list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
        list.add(new CityEvent("Droidcon", "Droidcon in Berlin", CityEvent.EVENT_TYPE));
        return list;
    }



public class CityEvent {

    public static final int EVENT_TYPE = 1;

    private String mName;

    private String mDescription;

    private int mType;

    public CityEvent(String name, String description, int type) {
        this.mName = name;
        this.mDescription = description;
        this.mType = type;
    }

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        this.mName = name;
    }

    public String getDescription() {
        return mDescription;
    }

    public void setDescription(String description) {
        this.mDescription = description;
    }

    public int getType() {
        return mType;
    }

    public void setType(int type) {
        this.mType = type;
    }
}

0 个答案:

没有答案