dqn48247 2015-01-28 04:34
浏览 316
已采纳

Laravel - 有全局变量吗?

I am using Laravel. I have referenced my project name in several different places in my views.

I then decided to change the name of my application! So is there a global variable I can use instead and then I only have to change the name in one place next time?

I looked through the docs but couldn't see this feature..

  • 写回答

2条回答 默认 最新

  • dongyuling0312 2015-01-28 05:39
    关注

    Of course global variables exist in Laravel as well. However just because the exist it doesn't mean you should use them.

    A much more suitable approach would be to store the name in a config file.

    You can use app/config/app.php and just add a row:

    return array(
        'name' => 'FooBar',
        // existing values ...
        'debug' => false,
        'url' => 'http://localhost',
        // etc ...
    );
    

    Or create your very own config file. For example app/config/site.php

    return array(
        'name' => 'FooBar'
    );
    

    And you use it like this:

    Config::get('app.name');
    

    Or

    Config::get('site.name');
    

    See the documentation for more information

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?