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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题