dongyi5570 2014-12-15 21:38
浏览 33

如何在Laravel 4中使用数组中的If-else?

I am using Laravel 4.

in app/config/prod/app.php

I have an array of things, laid out like this,

<?php

return array(

‘debug’ => “fault”

// and more ...

)

I want to set my default value to a different thing base on auth user type.

IF the user type is admin, I want to set the debug to true ELSE set it to false.

Can I do an if-else check within an array in PHP ?

I’ve tried using ternary operator, but no luck. I hope I didn’t do anything wrong.

isset( Auth::user()->type == “Admin” ) ? ‘debug’ => “true” : ‘debug’
 => “fault”

Thanks for your help in advance ! :)

  • 写回答

2条回答 默认 最新

  • douque9815 2014-12-15 21:42
    关注

    What you can generally do is use the ternary operator for the value and not the full key value pair

    'debug' => (Auth::user()->type == "Admin" ? true : false)
    

    However, I'm not sure what is available when this config gets loaded (Auth::user() is probably not). Let me know if this works and I'll come back to you.

    Update

    A little test just confirmed my assumption. The Auth class is not available when config files get parsed. That means you will have to set the value afterwards. I suggest you leave the default to 'debug' => false and use this if the user is admin:

    if(Auth::user()->type == 'Admin'){
        Config::set('app.debug', true)
    }
    

    You can do this wherever you want as long as it's "early enough". For example you can use the global App::before (in filters.php)

    App::before(function($request){
        if(Auth::check() && Auth::user()->type == 'Admin'){
            Config::set('app.debug', true)
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?