正在 PHP 中解析 Laravel 配置

Parsing Laravel Config in PHP

Laravel 的配置文件非常适合存储从我网站上的每个页面访问的变量。当前 app.php 配置文件包含:

return [
    'name' => 'This is a name',
    'slogan' => 'This is a slogan!',
    'primary_color' => '#cc0202',
    'secondary_color' => '#990000',
    'tertiary_color' => '#FFFFFF'
];

这只是一个关联数组,Blade 代码可以使用以下方式访问它:

{{ config('app.primary_color', '#cc0202') }}

但是我希望使用 public 文件夹中的 .php 文件中的数据。 public 文件夹中的文件不能使用 Blade。获取此数据的最佳方式是什么?

试试这个

<?php $variable = config('app.primary_color', '#cc0202') ?>

访问和打印:

echo config('app.primary_color');

设置数据:

config(['app.primary_color' => '#cc0202']);

为什么不改用自定义服务提供商?

来自 laravel 文档:

Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel's core services are bootstrapped via service providers.

https://laravel.com/docs/5.3/providers , You can see this helpful tutorial on laracasts to: https://laracasts.com/series/laravel-5-fundamentals/episodes/25

如果您想在 public 目录的 PHP 文件中使用您的 Laravel 的配置,只需通过

获取此文件
$config = include '../config/app.php'; // Go back to main catalog
                                       // and go to config catalog

并将其用作普通数组。

例如

echo $config['primary_color'];

但在我看来,如果您使用的是框架,将 PHP 个文件放入 public 目录是非常糟糕的做法。