Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

melis's avatar

Laravel 5 Global Variables

Hello, I was wondering where is the most appropriate place for defining global variables in Laravel 5?

0 likes
15 replies
milon's avatar

I'd like to place them on config file, don't know whether it is perfect or not. Some like to store them on .env file as environment variable.

bashy's avatar

Well the answer you accepted is exactly the same as that (environment vars).

Edit: Okay delete your reply :P

pmall's avatar
pmall
Best Answer
Level 56

You can create a new file in config and use

config('your_new_file_name.key')
3 likes
slovenianGooner's avatar

I'd go with a config file. The idea behind a global variable is that it's gettable and settable from everywhere in the application, which is what the Config alias provides.

Afaceri's avatar

If you have a small app maybe you will fill confortable to store your globals as define('THUMBS_DIR','thumbs'); in your route file on app/Http/routes.php

1 like
eugenefvdm's avatar

I'd go with a config file. The idea behind a global variable is that it's gettable and settable from everywhere in the application, which is what the Config alias provides.

I tried that. The problem appears to be blade cache these variables so even though you can change them, they don't reflect on the blade.

Gerard's avatar

This config file should be inside de config folder in order to work?

eugenefvdm's avatar

The idea I get is this whole concept of global variables and (dynamic) global configuration in Laravel is a bit of a minefield. Someone should take the effort to properly document all the different ways, or the Framework should be improved to accommodate something all of us want to do at some time.

gbrits's avatar

Even though this pointed me in the right direction, it still didn't solve my problem - so I thought I'd post further to the accepted answer, rather than posing different solutions.

Once you have created your new config file, for example config/urls.php and you added in your array of keys and values, for example:

return [
    'home' => 'http://www.google.com'
];

You now need to run php artisan config:cache in order for you to access that variable {{ config('urls.home') }}

jessicabarnes's avatar

First, you have to create global.php file in config folder of your laravel 5 application. I did add three variable in that file : siteTitle, pagination & tagLine. you can also add more than one on that file.

For Example: return [ 'siteTitle' => 'HD Site', 'pagination' => 5, 'tagLine' => 'Do the best' ];

After creating this file you can use using config() helper. In below a line, you can put anywhere and get "siteTitle" value, the same way you can get other value too.

{{ config('global.siteTitle') }}

1 like

Please or to participate in this conversation.