You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Laravel 5.6中创建全局变量?类中全局变量全项目访问方案问询

Hey there! I've worked with Laravel 5.6 quite a bit, so let's walk through the best ways to set up global variables—both simple ones and class-based variables you can access anywhere in your project. Here are the most reliable approaches:

方法一:利用配置文件(最简单的全局变量方案)

Laravel's config system is perfect for static global values that don't change often. You can either use existing config files or create a dedicated one:

  1. Create a new config file at config/globals.php:
// config/globals.php
return [
    'app_short_name' => 'Laravel56Demo',
    'support_email' => 'help@myapp.com',
    'max_file_upload' => 5242880 // 5MB in bytes
];
  1. Access it anywhere in your project:
  • In controllers/models: config('globals.support_email')
  • In Blade views: {{ config('globals.app_short_name') }}

Pro tip: If you modify this file in production, run php artisan config:cache to update the config cache.

方法二:Share variables directly with all views

If you want variables available in every Blade template without calling config() every time, use a service provider:
Edit app/Providers/AppServiceProvider.php and update the boot() method:

public function boot()
{
    // Share a single variable
    view()->share('site_copyright', '© ' . date('Y') . ' My App');
    
    // Share multiple variables at once
    view()->share([
        'site_title' => 'My Laravel 5.6 Project',
        'contact_phone' => '123-456-7890'
    ]);
}

Now you can use these variables directly in any view: {{ $site_title }}

方法三:Create a globally accessible class

For class-based global state or dynamic global values, build a custom service class:

  1. Create app/Services/GlobalSettings.php:
namespace App\Services;

class GlobalSettings
{
    // Static properties for direct access
    public static $app_version = '1.0.0';
    public static $maintenance_mode = false;

    // Static methods for dynamic global logic
    public static function getCurrentUserCount()
    {
        // Example: Fetch real-time user count from DB
        return \App\User::count();
    }
}
  1. Access it anywhere:
  • In controllers/models:
    use App\Services\GlobalSettings;
    
    echo GlobalSettings::$app_version;
    echo GlobalSettings::getCurrentUserCount();
    
  • In Blade views: {{ \App\Services\GlobalSettings::$app_version }}
方法四:Use a Facade for cleaner access

If typing the full namespace feels tedious, wrap your class in a Facade for shorthand access:

  1. Create app/Facades/Settings.php:
namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class Settings extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'settings'; // Name to bind in the service container
    }
}
  1. Bind the class in AppServiceProvider.php's register() method:
public function register()
{
    $this->app->bind('settings', function () {
        return new \App\Services\GlobalSettings();
    });
}
  1. Add the Facade alias to config/app.php in the aliases array:
'aliases' => [
    // ... existing aliases
    'Settings' => App\Facades\Settings::class,
]
  1. Now you can use it globally with just the Facade name:
    Settings::$app_version or Settings::getCurrentUserCount()
Quick Notes
  • Static properties are reset on every request (great for per-request state, but not for cross-request persistence—use cache/database for that).
  • For environment-specific globals, use .env variables and reference them in config files with env('VARIABLE_NAME', 'default').

内容的提问来源于stack exchange,提问作者reza baghiee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:04:48