Laravel 4: Confused about how to use App::make()(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('MyNewClass', $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 论坛的好心人为我解答了这个http://laravel.io/forum/02-10-2014-laravel-4-confused-about-how-to-使用-appmake
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('MyNewClass', function() use ($classArgs) {
return new MyNewClass($classArgs['id'], $classArgs['title']);
});
// get the binding
$newClass = App::make('MyNewClass');
这篇关于Laravel 4:对如何使用 App::make() 感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 4:对如何使用 App::make() 感到困惑
基础教程推荐
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
