# \*\*SOLVED\*\* Injecting the $container object into a class handling a route

**URL:** <https://discourse.slimframework.com/t/solved-injecting-the-container-object-into-a-class-handling-a-route/4027>\
**Category:** Questions\
**Created:** [April 8, 2020, 4:44pm UTC](https://discourse.slimframework.com/t/solved-injecting-the-container-object-into-a-class-handling-a-route/4027 "2020-04-08T16:44:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jordanr](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@jordanr](https://discourse.slimframework.com/u/jordanr)\
**Post date:** [April 8, 2020, 4:44pm UTC](https://discourse.slimframework.com/t/solved-injecting-the-container-object-into-a-class-handling-a-route/4027/1 "2020-04-08T16:44:08Z")

</div>

Hi all,

i am learning the ins and outs of Slim Framework. For the purposes of illustrating the problem, I am trying to return the details of a user ID in json format.

**routes.php**

```
use Slim\App;

return function (App $app) {
$app->get('/user/{userID}', 'Gpmfg\User\User');
};

```

**User.php**

```
namespace Gpmfg\User;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class User{

  public function __construct(){
  }
  public function __invoke(Request $request, Response $response, $args){
    $response->getBody()->write(json_encode(array("id" => $args['userID'], "name" => 'Bob User')));
    return $response->withHeader('Content-Type', 'application/json');
  }
}

```

Very basic example, but it works. Where I am stuck is I have PDO settings defined in my settings.php:

**settings.php**  
use DI\ContainerBuilder;

```
return function (ContainerBuilder $containerBuilder) {
  // Application specific settings
  $containerBuilder->addDefinitions([
    "db" => [
      "host" => "mysql:host=myhost",
      "user" => "mydb",
      "pw" => "mypw",
      "config" => []
    ]
  ]);
};

```

I would like to insert a PDO object into my user class as defined above, where my \_\_invoke would query the database for details and return them. How do I get a PDO object into my user class? I tried passing in the $container object to the \_\_construct function, but I got the following error

```
Entry \"Gpmfg\\User\\User\" cannot be resolved: Parameter $container of __construct() has no value defined or guessable
Full definition:
Object (class = Gpmfg\\User\\User lazy = false __construct( $container = #UNDEFINED

```

Thank you.

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [April 8, 2020, 7:16pm UTC](https://discourse.slimframework.com/t/solved-injecting-the-container-object-into-a-class-handling-a-route/4027/2 "2020-04-08T19:16:58Z")

</div>

Hello @jordanr

You should add an container defintion for `PDO::class`. [Read more](https://odan.github.io/2019/11/05/slim4-tutorial.html#creating-a-repository)

Example:

```php
PDO::class => function (ContainerInterface $container) {
    // load database settings
    $settings = $container->get('settings')['db'];

    $host = $settings['host'];
    $dbname = $settings['database'];
    $username = $settings['username'];
    $password = $settings['password'];
    $charset = $settings['charset'];
    $flags = $settings['flags'];
    $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";

    // create and return PDO instance
    return new PDO($dsn, $username, $password, $flags);
},

```

Then in your class add PDO to the constructor:

```php
use PDO;

class User
{
  private $pdo;

  public function __construct(PDO $pdo)
  {
     $this->pdo = $pdo;
  }

  public function __invoke(Request $request, Response $response, $args)
  {
    $rows = $this->pdo->...;

    $response->getBody()->write(json_encode(array("id" => $args['userID'], "name" => 'Bob User')));
    return $response->withHeader('Content-Type', 'application/json');
  }
}

```

---

<div class="post-metadata">

**Author:** ![jordanr](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@jordanr](https://discourse.slimframework.com/u/jordanr)\
**Post date:** [April 8, 2020, 7:26pm UTC](https://discourse.slimframework.com/t/solved-injecting-the-container-object-into-a-class-handling-a-route/4027/3 "2020-04-08T19:26:21Z")

</div>

@odan,

Thank you! That worked perfectly. So going forward, I just need to make sure I have a definition for any class I want to inject. That makes perfect sense. I appreciate your help!
