Laravel define global constants Config php file
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!
Previous Stories You will Love:
Laravel call named routes in console internally php artisan command
How to call Laravel routes internally php artisan command
Telegram Bot Packages libraries Examples in Laravel, PHP, Node.js, Python, Java, Ruby, Swift, C++
If you want to learn more about Telegram bots, This Introduction to Bots would be a good guide to start.
Laravel Auth::routes() Email verification Reset password Authentication Registration routes
Auth::routes() is a helper class that helps you generate all the routes required for user authentication
Laravel eloquent model increment with or without updating timestamps
Problem: You may need to increment a field in a record of a table (Laravel Model) and do not want update the updated_at…
Laravel validation username no space allowed alpha_dash or regex custom rule
If you want to restrict the username in Laravel to only accept letters, numbers, dashes and underscores and not space…
Laravel delete queued jobs using redis horizon artisan command
The question is how to delete all jobs which are queued in Laravel when you are using redis as the Queue driver.
Laravel detector mobile browser name version platform device robot crawler user language
Laravel session flash message include html tag link href
In Laravel you may insert a or strong tags into the flash message deliver to the session.
Laravel 502 Bad Gateway when starting Laravel Valet — Simple solution
The first solution is here: