The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

How am I able to use Slim's DI container for all of my classes?

George's Avatar

George

10 Oct, 2015 12:51 AM

I was recently instantiating classes normally in the index but I saw someone doing this by using a singleton of the Slim DI container so I was wondering how to implement this.

I currently have tried doing this to connect to my database however I am not sure that it is connecting to it and also when I attempt to force an error which the PDO would usually spit out into my logs It is clear and there are no errors.

Index:
$app->container->singleton('db', function () use ($app, $stuff) {
return new Database($app, $stuff);
});

Class:
class Database{

protected $app;

public function __construct(\Slim\Slim $app, $stuff, $ary){
$this->app = $app;

$this->stuff = $stuff;
$this->ary = [];

try{
$this->db = new PDO($this->stuff, $this->ary);
return $this->db;
}catch(PDOException $e){
$this->app->log->error('error?');
return $this->app->error();
}

}

}

  1. 1 Posted by Jeremyoi on 10 Oct, 2015 05:09 AM

    Jeremyoi's Avatar

    wow, awesome article.Really looking forward to read more. Will read on... http://gjh5g34t5uwfeq4r.tumblr.com/ - Hafer

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac