Custom configuration files, keys, and env variables

Kati Frantz
A free video tutorial from Kati Frantz
Full stack software engineer
4.5 instructor rating • 6 courses • 62,810 students

Learn more from the full course

The Ultimate Advanced Laravel Pro course (incl Vuejs)

Learn the advanced concepts of the laravel and vuejs frameworks, and also build and deploy a complete real world project

11:50:45 of on-demand video • Updated December 2017

  • You will learn how to build laravel applications using TDD
  • You will learn how to build advanced vuejs applications with a laravel backend
  • You will learn how to integrate the stripe api into laravel and / or vuejs applications
  • You will learn how to work on a real world full stack backend application with git
  • You will learn how to deploy and manage a laravel application in production
English [Auto] The first customization trick we want to talk about is how to define your own costume configuration keys. For example if we had autoconfig them a tree they will provide us with a lot of default configuration files. And in each of these configuration files we have cause configuration keys. Now how do you define yours if you want to use them in your application. So let's say for example you're building an application and you actually want to set a key to define the developers that actually develop that application. A good place to place start is going to be in your Abdur ph be where we have the name of the application time zone you are all of that. Therefore right down at the bottom you might want to actually have a key that says Here are the developers of this application. This is just in our way. And what you can do is actually define your own specific key. Therefore write down before we have our way of life will provide us. I'm going to put in a key immediately after loff level and this key is going to be developers. And like I said this is an array. Therefore you can actually have a nested in here or you can have a string whatever you want to have in this case. I'm going to have an extra avi and this and I said are we going to be an array of developers therefore we can have front of cats that come with just like the email of one of the developers. And you can have multiple developers. Now let's say in a part of your application you actually want to display the developers that develop the application. I'm going to head over to my weather THP and we're going to see how this works just like any other configuration that level provides for us. You can go ahead and reference that key in the can fix and automatically going it gets it for you. So I'm going to define a new routes and I'm going to pass in a closure here and disclosure. We are going to simply die. Don't confuse value using the config helper that writes for us. We can reference the fire which is automatically referenced from the config directory and we can see in this conflict which would get me the app the PH B which would reference this and then for us to get this what's key we simply need to differentiate using a period so that developers and lovely's are going to return to us this way. So let's go ahead and visit that and see what we get. Vista custom route. And sure enough we have that our radar level returns to us therefore everyway in the application where you need to a friends house while you can use Lovaas helper method. And you want to talk about Satterlee having personal configuration files. For example if you're working on a project you might want to have everything that concerns Kauf Grecians for that project in a particular configuration file. Now how do you do this. Most people actually result in saving these values or settings into the database. Meanwhile a simple custom configuration file will do the trick. That's quite hard on see how this works. I'm going to create a new file in the config directory. And let's say we're building a block. So I'm going to call this a block b. And here we have this file member. It's just appear to be file that returns an array. So return an array. Now automatically if you want to fetch this file need to get the config help helper the block and then we can pass in whatever keys we want to save here. So for example this is something that I do in most of my projects if I have a project that needs just one or just two administrators then I'm going to have a configuration file called some configuration file for that project and I'm going to have a key there that says administrators. And then I'm going to pass in an array for the e-mails for those administrators and then those on the street are just going to have accounts in my database. Therefore let's say I can see that column to be an administrator and maybe I was friends at that Karl Moso to be an administrator. Let's just say different emos for different uses. What I'm going to do now is good. Any parts of my application and then reference this custom configuration file and this custom configuration key and I can get this right. So this is mostly useful if you want to have maybe an administration middleware but you don't necessarily want to have an administrator stable that knows who is administrator or not. So let's head over back to what can figure out and see if we can get this value. Remember we need to go to the file which is block and we need to get that particular key which was administrators. OK. So let's see if level is going to give this to us refresh. And there we go. Level gives us that exact same rate. So now we see the amount of legs reaching that level gives us with our configurations. Now let's talk about our default done in the fall. Most people are scale of actually starting this fall. But level simply gets this volume in the conflagrations. Therefore you can actually define your own custom conflagrations in this debate in the fall. Now let's go ahead and see an example. Let's say we want to actually define create all or up create all for this application and I'm going to said that to me. So I'm going to see friends made that come to me the e-mail of the creator of the application. Now when I do find this constant anywhere in my application I can actually get it. For example for head to my web the HP I pull out this kind of fake help. And I referenced NVI I can get up create all of this mid-thought is simply going to get that configuration environment variable. So if I to my boss a quick refresh there it gives me that exact string. Bert Lovelle does something behind the scenes for us. It has a cache for the conflagrations therefore referencing EMV is actually skipping Lovell's configuration. You don't want to do this in your project for example using NVI interviews or NVI in your Rauch's file or control us. It's really going to slow down your application because all the time it's going to go into the environment and try to get this value. OK what you want to do rather is to have a custom configuration key. Now that we have a block that BHB we can have another key that says Create all. And we can call it in the method and with reference to our Creator or key. OK. And what happens is since it's in a configuration file level is automatically going to cache this value so every time you need it you just referenced the config mid-thought and it's going to get it from cache if it exists. Ok so now rather than referencing envy we want to reference config for the config block file dot creator or key. OK. Quick refresh to see what we have. And we have the exact same thing but now the advantage is this value has been crushed and our obligation is really going to give us that faster. As you can see so we've gone through four major conflagrations. First we should be able to define our own custom keys in any of the configuration files that we have. Second we should also be able to create our own custom configuration files in applications. Third we should also be able to create our own custom EMV keys which we can reference from any of the configuration files and most importantly we remember never to use the envy file in our applications directly. But you should actually reference lava's conflagration which is going to do the caching for us.