Configuration files make it easy to access the config options across the application. In Laravel there is no exception. It is very clean approach keeping the all config items in one place.
Create a Config file
To create a custom config file in Laravel, go to config directory and create a php file. In my case I create a file called image.php, to keep various image dimensions.
After creating the file you can return an empty array
<?php return [];
Adding config options
We got our config file, now we want to add some options into our file
<?php return [ 'large_image' => 500px, 'small_image' =>100px ];
We can even use multidimensional arrays.
<?php return [ homepage =>[ 'large_image' => '500px', 'small_image' =>'100px' ], product =>[ 'large_image' => '900px', 'small_image' => '250px' ] ];
Using config options
We saw that above methods are for setting the config items in Laravel. Now these new methods are to retrieve value from config options.
To get the large image size for product you can write like this
echo Config::get('image.product.large_image');
So the first segment is the config file name, and rest is associate array key and values.
Setting a config value on runtime
When you want to change the config value after the application boots.
Config::set('image.product.large_image','1000px');
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.
Leave a Reply