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.
Laravel 5 Global Variables
Hello, I was wondering where is the most appropriate place for defining global variables in Laravel 5?
Global meaning http://php.net/manual/en/reserved.variables.environment.php ?
Well the answer you accepted is exactly the same as that (environment vars).
Edit: Okay delete your reply :P
You can create a new file in config and use
config('your_new_file_name.key')
I made a simple post in step by step to define global variable in laravel, It will help some one
Since i found after long time googleing
Read this tutorial http://www.techigniter.in/tutorials/defining-global-variables-in-laravel-5/
Everyone's answer appears to be global contants
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.
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
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.
This config file should be inside de config folder in order to work?
If you want to read your settings from a table instead, I found this post to be interesting: http://stackoverflow.com/questions/34126578/laravel-set-global-variable-from-settings-table
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.
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') }}
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') }}
Please or to participate in this conversation.