How to Use Environmental Variables (.env) Files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys hey going so today I'll be covering m-files so essentially n files are used to store sensitive information such as credentials for your app and typically the information or that data should not be seen by the end user and a few examples of that data is going to be things like database login credentials API keys or things like API endpoints etc and it's also worth noting that this data is typically gonna be environment specific so you know if your app is running on different environments most of the time some of this data is going to be changing depending on of course the environment so let's just go inside the text editor right here I'll be showing you an example of a dot n file and then I'm going to be showing you an example in PHP in order to actually access that data but of course my example can be used across different languages except it's going to be slightly different of course depending on the language but anyway let's go inside here now and create a new dot M far so I'm sure you may have seen it before essentially this file looks like this dot simple um okay and inside here of course this is where you store your information so for example let's say that the application I'm doing or creating is interacting with an with a weather API okay so we would say for example inside here weather underscore API key then we can say equal to and then of course right here we can say ABC one two three and that is my weather API key and typically you'll find that the dot end API sorry I did not have data or the key so there's a key value pair so the actual name of the environmental variable so with our API key typically these are going to be in uppercase and the data is of course gonna be in whatever you know case it needs to be but essentially that right there is how to define a piece of data using M and then if I make some more you know include some more information we can say for example database credentials I can say DV underscore username equal to decode so database username then I can say for example DB password equal to password 1 and then even something like a DB underscore ports equal to and then a random port number here so we can see how across different environments if you know 20 people avoiding this app you know the DB port might change across different systems so that's basically right there an example of a dot M file and the data that may be stored inside of it so now that we have this let's go inside PHP and actually access this information so let's go inside here make a new index dot PHP file and I'm also going to be using the PHP dot end library so let's go inside here now inside the command line and install the PHP library so we can say here using composure composure require then it's V V Lucas I believe his name so Lee Lucas PHP M press ENTER now and this is going to install I think one of the most common if not the most common PHP library so once this is done installing of course I can show you how to use it but as I said different languages are gonna have different ways and different libraries to access these files but anyway it is all done so now I'm gonna go inside PHP and I'm going to require the auto loader so I can just say require real earth D PI bender and then autoload the PHP and that is the autoloader for for composer so now to use this lot in library I can say use em n dot m and I can say but in equal to type and then I can say creates immutable and pass it near the directory huh and then I can say dot inven in load and that right there in PHP is going to load all of the environmental variables from that file into V get and function or you can access it using the get em function but of course like I said it's gonna change depending on the language but I do want to point out that our passing in the directory here and the reason why knows to look at this dot n file right here is because of the name of it so dot M R is a standard name for your environmental variables if you don't call your files this name then certain libraries and may have trouble actually picking up your name or your file sorry but anyway now I know that the dot M far has been loaded I can go inside here and say for example make a new variable weather API key file I can say get em and then passing me up weather API key and then of course refers to my key my key value pair here Sookie there and that now if I pass in this I'm gonna get here at the weather API key value so now if I say about duck and passed in here with our API key we should see in the in the in the console yeah I'm actually using echo for this that might change so anyway let's do echo here to print out the value I'm going to say PHP then index dot PHP and we can see right there we get ABC one two three so you know you might want to of course use this variable now to connect to your API and that's the basic usage of dot in thought but I do want to mention one last thing and that is typically if you're if a if a project is you know tracked using git or something like that you're gonna have a separate file which is committed to the master branch or whatever and that's gonna have sorry that's gonna be dot and dot example and this file right here is an example dot M file and typically you're gonna find the exact same key value pairs or environmental variables inside this file compared to what you have as your actual dot m so typically you don't commit your dot M file because these are specific to your machine however you do commit a dot m dot example because that one is purely an example and of course here your API key might be you know example API key something like that something not real but it may also contain defaults that way when you actually pull down or clone the branch then you're gonna have data there or environmental variables that you know don't need to change but typically you're gonna have your dot m dot example and when you pull a repo we're gonna only have that done I am that example and if you want to of course and make a dot M you should be a copy it and you paste it and you may name it to be done in and you're good to go so that is how to use dot M files thank guys for watching and I'll see you later
Info
Channel: dcode
Views: 45,401
Rating: undefined out of 5
Keywords: code, coding, programming, tutorial, introduction, beginner, walkthrough, guide, software, development, simple, easy, english, with, example, examples, developer, lecture, recording, how, to, web, website, app, application, project, .env, environmental, variables, ini, settings, database
Id: txGL-Ld9zD8
Channel Id: undefined
Length: 7min 32sec (452 seconds)
Published: Sun Jan 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.