douchen7324 2014-11-16 01:27
浏览 215

如何在Laravel中定义全局变量?

I use to develop with Drupal. In Drupal there are global variables such as $user or $_view, so you can use these in different modules. I wonder how can I make something like this in Laravel, after the user log in, I can use the global $user in different controller.Except using session is there any other ways to implement this one? Thank you.

  • 写回答

1条回答 默认 最新

  • dousui6488 2016-07-22 12:21
    关注

    For most constants used globally across the application, storing them in config files is sufficient. It is also pretty simple

    Create a new file in the app/config directory. Let's call it constants.php

    In there you have to return an array of config values.

    return [
        'langs' => [
            'es' => 'www.domain.es',
            'en' => 'www.domain.us'
            // etc
        ]
    ];
    

    And you can access them as follows

    Config::get('constants.langs');
    // or if you want a specific one
    Config::get('constants.langs.en');
    

    And you can set them as well

    Config::set('foo.bar', 'test');
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料