Laravel – Configuration

Let’s learn config function through a project. Config function will go into a config directory, file place in config directory. Config function is as-:

Whatever you will write or supply in config function it will going to look up for those values.

– Reason for using config function instead of env is that when you cache your configuration or your env file, PHP is going to turn all of the respective text into an array to make it quicker.

– So, now load up your console and you could run PHP artisan to see all the different items and on the top you will see config cache as-:

Here config cache SS creates a cache file for faster configuration loading.

Now give command “php artisan config:cache”

– Once it’s done, then if you look different files in config as-:

– Inside bootstrap cache you will have different files there.

– If you look in services.php you can see that you have all services there.

– If you look in packages file, you will see all the different packages that Laravel has automatically detected that you have installed.

– And finally if you look the config file in cache, you will found the gigantic array which is actually from config directory.

It means that if you have your configuration cached or you will something like ‘app name’ in .env file obviously.

Once you save the file and open your project or website in browser then the name will not changed there because configuration is actually cached, so result is that when you update your configuration, you want to make sure that you run the artist in command either “PHP artisan config clear” or you could get away with config cache which would clear the cache and then cache it again. Now if you see the browser and refresh the website you will see the changed name which means items have been updated now.

Suppose if you wanted to create new configuration file, there is a bunch of different reasons why you like or why you might create one but

– If the concept is that you are dealing with some third-party service and you have some keys that you want to store so that they can config directory.

– Create new file and save it with name by pretending the service called as cats. Suppose Name as cats.php.

– Now write a code in file to continue the project.

– Enter the specification need as-:

Other config files you could have some information in .env file. So you could say cat age give as ‘5’.

Now come back to cat.php and change the age to call from .env file as-:

So it will refer to your environment variables.

Now you are done with how you access this well.

Come to your project file which you supposed as “welcome.php”. Let’s do underneath the app name. write in another < div> as-:

	< div>
	< p>
	     Cat-Name:-{-config (‘cats.name.first’)-}}-{{-config(‘cats.last.first’)-}}
	< /p>
	< p>
	     Cat-Age:-{{-config(‘cats.age’)-}}
	< /p>
	< /div>
	

So here you have configuration cache. Now refresh the browser page of project. You will see that there is no information.

– Now Got to console and do “php artisan” command.

– Let’s do “config clear” and when you refresh you can see that you have that information.

– To make changes just change the value in .env files and update it.

Just remember when you don’t cache the configuration, just edit env file and see result automatically.

– As env file is too large so the processing of Laravel will somehow get slow. So figure out which variable you are referring to and then return that information.

Copyright 1999- Ducat Creative, All rights reserved.