donglao4370 2016-10-29 19:18
浏览 37
已采纳

如何在Slim 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.

  • 写回答

1条回答 默认 最新

  • dourun2990 2016-10-29 19:26
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条