如何在Slim 3.1依赖注入中触发口述经理 [英] How to trigger Eloquent Manager in Slim 3.1 Dependency Injection

查看:74
本文介绍了如何在Slim 3.1依赖注入中触发口述经理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ container ['db']我已经编写了我的代码来实例化Eloquent Capsule / Manager = function($ c){
$ settings = $ c-> get('database');
$ db = new \Illuminate\Database\Capsule\Manager;
$ db-> addConnection($ settings);
$ db-> setAsGlobal();
$ db-> bootEloquent();
return $ db;
}

我有这样的路线

  $ app-> get('/ adduser',function(){
$ user = new Users;
$ user-> name =Users 1;
$ user-> email =user1@test.com;
$ user-> password =My Passwd;
$ user-> ; save();
echoHello,$ user-> name!;
});

当我在浏览器中运行路由时,会在Web服务器错误日志


PHP致命错误:调用/ home / *** / vendor / illuminate / database / Eloquent中的非对象上的成员函数connection()在第3335行的Model.php


在我看来,这是因为雄辩的Capsule / Manager没有被DI触发实例化。



我发现一个解决方案来解决这个问题,通过使用这样的自定义构造函数声明模型

 code>使用Illuminate\Database\Eloquent\Model作为雄辩; 
使用Illuminate\Database\Capsule\Manager作为Capsule;

class用户扩展Eloquent {
protected $ table ='users';
protected $ hidden = array('password');

public function __construct(Capsule $ capsule,array $ attributes = array())
{
parent :: __ construct($ attributes);
}
}

但我不认为这是一个干净的解决方案,因为我必须使用自定义构造函数重写所有我的模型。



我需要帮助找到我的问题的解决方案。
我尝试使用以下代码:

  $ app-> get('/ adduser',function()使用($ some_variable){
//创建用户脚本
});

但到目前为止,我不知道如何触发 $ container [' db'] 使用此方法。我真的很感激在这里的帮助。

解决方案

将胶囊管理器注入每个模型可能不是一个好主意..当你说你自己,这将是一个痛苦的管理。
你在封闭之外尝试过这个代码吗?即。在你的应用程序的引导部分。

  $ db = new \Illuminate\Database\Capsule\Manager; 
$ db-> addConnection($ settings);
$ db-> setAsGlobal();
$ db-> bootEloquent();

setAsGlobal 函数使Capsule Manager实例静态,所以模型可以访问它全球。
只是要注意,约定是以单数形式命名你的模型类。即。用户而不是用户。


I have write my code to instantiate Eloquent Capsule/Manager using slim DI like this

$container['db'] = function ($c) {
    $settings = $c->get('database');
    $db = new \Illuminate\Database\Capsule\Manager;
    $db->addConnection($settings);
    $db->setAsGlobal();
    $db->bootEloquent();
    return $db;
}

And I have my route like this

$app->get('/adduser', function() {
    $user = new Users;
    $user->name = "Users 1";
    $user->email = "user1@test.com";
    $user->password = "My Passwd";
    $user->save();
    echo "Hello, $user->name !";
});

When I run the route in browser it will produce error in web server error log

PHP Fatal error: Call to a member function connection() on a non-object in /home/***/vendor/illuminate/database/Eloquent/Model.php on line 3335

In my opinion this is happened because the Eloquent Capsule/Manager is not triggered to be instantiate by DI.

I found a solution to solve this by declare the Model with custom constructor like this

use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Capsule\Manager as Capsule;

class Users extends Eloquent {
    protected $table = 'users';
    protected $hidden = array('password');

    public function __construct(Capsule $capsule, array $attributes = array())
    {
        parent::__construct($attributes);
    }
}

But I don't think this is a clean solutions, because I have to rewrite all my Models using custom constructor.

I need help to find solutions for my problem. I try to use code below:

$app->get('/adduser', function() use ($some_variable) {
   // create user script
});

but so far I don't know how to trigger $container['db'] using this method. I really appreciate a help here.

解决方案

It's probably not a good idea to inject your capsule manager into each model.. As you say yourself, that's going to be a pain to manage. Have you tried this code outside of the closure? ie. in the bootstrap part of your app..

 $db = new \Illuminate\Database\Capsule\Manager;
 $db->addConnection($settings);
 $db->setAsGlobal();
 $db->bootEloquent();

The setAsGlobal function makes the Capsule Manager instance static, so the models can access it globally. Just to note, convention is to name your model classes in singular form. ie. User rather than Users.

这篇关于如何在Slim 3.1依赖注入中触发口述经理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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