gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 23:52:23 24 4
gpt4 key购买 nike

将变量添加到可跨 Controller 和 View 访问的 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') }}

关于php - 在 Laravel 5 中设置全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36923763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com