Newbie2016 Newbie2016 - 1 month ago 9
PHP Question

how to set a property in silm 3

I am trying to add a property in Slim 3 container, but when i var dump it shows me "Notice: Undefined property: Slim\App::$user". I know in Slim 2 it is :

$app->container->set('user',function(){
return new User;
});

var_dump($app->user);


This will then show me all property in the user model container. However I am trying to achieve this in Slim 3.

I looked around the documents and found an add function but it returns an undefined property.

This is the code i tried:

$app->add('user',function(){
return new User;
});
var_dump($app->user);


I know the User class does exist because i have tested that. I am stuck at this point and i am not sure what the method is to add a property to my container.

Answer

Its doesn't automatically add it as property when you register it on the DI container, you can just add it as a property with:

$app->user = new User;

Or with DI

$app->user = $app->container->get('user');
Comments