Laravel 4:对如何使用App :: make()感到困惑 [英] Laravel 4: Confused about how to use App::make()

查看:1526
本文介绍了Laravel 4:对如何使用App :: make()感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循本文 http:中概述的存储库模式: //code.tutsplus.com/tutorials/the-repository-design-pattern--net-35804#highlighter_174798 而且我正在尝试使用App :: make()在Laravel中实例化一个类(我在猜测是Laravel的工厂模式吗?),我正在尝试解析类的参数,但是我不知道该怎么做.

I am trying to follow the repository pattern outlined in this article http://code.tutsplus.com/tutorials/the-repository-design-pattern--net-35804#highlighter_174798 And I am trying to instantiate a class in Laravel using App::make() (Which I am guessing is Laravel's factory pattern?) and I am trying to parse arguments to my class but I can't work out how to do it.

代码:

namespace My;

class NewClass {
    function __construct($id, $title) 
    {
        $this->id = $id;
        $this->title = $title;
    }
}

$classArgs = [
    'id'    => 1,
    'title' => 'test',
]

$newClass = App::make('My\NewClass', $classArgs);

谁能指出如何使用App :: make()的示例,或者我朝完全错误的方向前进,不应该使用App :: make()?

Can anyone point to an example of how to use App::make() or have I gone in the completely wrong direction and shouldn't be using App::make()?

推荐答案

Laravel论坛中的好人为我回答了这个问题

The good people in the Laravel forum answered this one for me http://laravel.io/forum/02-10-2014-laravel-4-confused-about-how-to-use-appmake

如果您可以将自定义实例化代码与App :: bind();绑定在一起,那就差不多了.像这样

Pretty much if you can bind custom instantiation code with App::bind(); like so

App::bind('My\NewClass', function() use ($classArgs) {
    return new My\NewClass($classArgs['id'], $classArgs['title']);
});

// get the binding
$newClass = App::make('My\NewClass');

这篇关于Laravel 4:对如何使用App :: make()感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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