加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – 在Laravel 5中设置全局变量

发布时间:2020-12-14 19:36:39 所属栏目:大数据 来源:网络整理
导读:将变量添加到可跨控制器和视图访问的Laravel框架的最佳方法是什么? 我不想使用.env来存储变量,因为它不能通过Git获得. 解决方法 您可以在config文件夹中创建文件.例如 config|- constants.php 在constants.php中,您可以定义全局变量 ?phpreturn [ /* |-----
将变量添加到可跨控制器和视图访问的Laravel框架的最佳方法是什么?

我不想使用.env来存储变量,因为它不能通过Git获得.

解决方法

您可以在config文件夹中创建文件.例如

config
|- constants.php

在constants.php中,您可以定义全局变量

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | User Defined Variables
    |--------------------------------------------------------------------------
    |
    | This is a set of variables that are made specific to this application
    | that are better placed here rather than in .env file.
    | Use config('your_key') to get the values.
    |
    */

    'company_name' => env('COMPANY_NAME','Acme Inc'),'company_email' => env('COMPANY_email','contact@acme.inc'),];

您可以使用以下两个函数之一访问这些变量:

Config::get('constants.company_name')
config('constants.company_name')

对于Blade也是如此:

{{ config('constants.company_email') }}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读