If you look at the main Laravel directory you can find these subdirectories:
If you open the config sub-directory you can see it includes of some files:
If you open each config file you see such pattern :
<?phpreturn ['A' => valueA,'B' => valueB,];
It means if you want to define global constants you should make your own config file in the config sub-directory similar to the above pattern.
For an example the content of config/app.php file is:
<?phpreturn ['name' => env('APP_NAME', 'Laravel'),'env' => env('APP_ENV', 'production'),];
Whenever you need to access “name” or “env” constants you have two options:
Using helper function (reference link)
The configuration values may be accessed using “dot” syntax, which includes the name of the file and option you wish to access. Since these constants are stored in the app.php file you should use “app.name” and “app.env ”to retrieve them.
Using Laravel Config:: facade
And don’t forget to include at the top of the controller this:
Or you may prefer the below style:
After making your own config file in config sub-directory don’t forget to do this:
php artisan config:cache
If you want to set configuration values at runtime such as value= ‘test’ for the constant bar in the file config/foo.php, do this:
or by passing an array of key / value pairs to the helper function:
config(['foo.bar' => 'test']);
Imagine you prepared a config file like foo.php with this content:
<?phpreturn ['bar' => [ 'A' => valueA, 'B' => valueB,
In order to access the value of “A” do this:
and for “B”:
Thank you for reading! If you enjoyed this article:
Clap it ! Share it! Follow Me in Medium!
Panjeh — Medium
Read writing from Panjeh on Medium. Physicists PhD, Self-taught web developer #Learning, #laravelphp #Vuejs. Every day…
Also I’d like to hear your opinion on this article. If you have any doubt, question or suggestion please leave a comment below.
Have a very wonderful day!