如何在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:
- 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 ];
- 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:cacheto update the config cache.
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 }}
For class-based global state or dynamic global values, build a custom service class:
- 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(); } }
- 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 }}
If typing the full namespace feels tedious, wrap your class in a Facade for shorthand access:
- 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 } }
- Bind the class in
AppServiceProvider.php'sregister()method:
public function register() { $this->app->bind('settings', function () { return new \App\Services\GlobalSettings(); }); }
- Add the Facade alias to
config/app.phpin thealiasesarray:
'aliases' => [ // ... existing aliases 'Settings' => App\Facades\Settings::class, ]
- Now you can use it globally with just the Facade name:
Settings::$app_versionorSettings::getCurrentUserCount()
- 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
.envvariables and reference them in config files withenv('VARIABLE_NAME', 'default').
内容的提问来源于stack exchange,提问作者reza baghiee

