This episode is for members only

Sign up to access "Build Your Own PHP Framework" right now.

Get started
Already a member? Sign in to continue
Playing
12. Using env files

Episodes

0%
Your progress
  • Total: 4h 45m
  • Played: 0m
  • Remaining: 4h 45m
Join or sign in to track your progress

Transcript

00:00
When you build any application, it's a really good idea not to include config directly in your code and instead have an individual file, one that exists on your local machine and one that exists on any other environment that you deploy to with different config values. Now, at the moment,
00:18
we have got our name of our app registered directly within our code. What we're going to do is switch this up to use environment variables so we can read these locally and on the server with different environment files. Now, if you've not worked with EMV files before, this might be a little bit confusing, but I'll do my best to explain how this works. OK, so just to test this
00:41
out, I'm also going to add in a debug option in here. So I'm going to say debug true since we're working on a local environment. If we deploy this to production, we don't want debugging on because if something goes wrong, we don't want to show errors to the users of our application. So how are we going to switch this up to change this value when we're in a different environment?
01:05
Well, that is using an EMV file. So in the root of our project, we're going to create out a new file called dot EMV. And in here, we can define out any environment variables that we want to register. So, for example, app name, we're going to call this no framework and really importantly, app debug. And on the local machine, we're going to set this to true so that they are our
01:31
environment variables now. Now, locally, we can keep this EMV file and then in production, we can create an EMV file in the root of our project specifically on our server and change this over to false. And we don't want to commit this to any source control, which is why in the get ignore file that I created a little bit earlier off camera, we have EMV being ignored. So this won't be pushed
01:56
and people won't see any of our configuration values. OK, so we've got our EMV file, but the question is, how do we actually use this? Well, if we go over to this PHP dot EMV library, we're going to pull this in. This will read all of our environment files for us and then we can start to use them in our apps. So let's see how this works. OK, so we'll first of all go ahead and
02:18
pull in the package. OK, so let's go ahead and pull this into our project and just wait for this to finish. And then over in Bootstrap and app where we're doing the majority of our setup stuff, the first thing that we want to do up here is load in these environment variables. So let's say dot EMV and we'll use this new library dot EMV just here and just make sure we pull this in at
02:41
the top here if you haven't already. And we're going to create an immutable with the location to our EMV file or the location where this actually lives. So this is just back a directory. So let's go back a directory into the root of our project and then we can say dot EMV and load. That is all we need to do now. So what we have done is this package is now going to load in our
03:05
EMV values. They will exist as environment variables on our server, either in local or production. And then we can use a helper to actually pull these in. So as an example, let's say EMV app name. Let's kill the page here and we'll just go ahead and dump this out. Like this. And if we head over to our app, there we go. So that's the name of our app just by using
03:32
this EMV helper from the package. So now what we can do is over in our config, some of these values are OK. So the name of your project isn't really sensitive. We don't really care if we're pushing that to source control or if that exists within our code, but we can switch this over regardless. So let's say EMV and app name and really importantly, debug app debug. And there
03:59
we go. So now that's going to work in exactly the same way. If we go over to our bootstrap file here and we just var dump on any of our config. So let's grab from the container our config instance and we'll say get app debug. That should be true, which is just here. But then over in our EMV file on the production environment or any other environment,
04:26
if we change this to false, that's still going to grab that value back. So what we're doing is combining config like this, which is really useful just for fluent access to config files. And we're combining that with reading directly from EMV files. OK, now that we've done this, what we can do is look at something that we mentioned earlier, and that is over in our
04:50
app service provider. We only want to do this when debug is enabled. We don't want to register ignition on a production environment when debug is set to false because otherwise we'll be showing errors to the user. So in here, what we can do is just wrap this in an if statement. So pull this into the block here and we're going to grab our container. So let's say this get
05:12
container or we can say get or just container. So that will just give us our container back and we can say get config. And we can say get app debug. So if this returns true, then we register ignition. Let's go over and give this a refresh. And I think let's just say get container and try that again. OK, yeah. So nothing is showing at the moment. So make sure we import
05:39
our config class. And there we go. Great. So this is now only going to be available when debug is set to true. Let's test that out by just going over and causing an error. We could probably do that over in our core app file here. So let's again go ahead and dump on something that doesn't exist. So let's say this name. Let's give that a refresh. We get our nice ignition error page.
06:05
But like I said, on a production environment over in EMV, if this is set to false, we should now not see anything. Error reporting is turned off and it just fails gracefully. And of course, we can fix that up whenever we get a chance. OK, let's go back over to core and app, get rid of this var dump here, and we now have environment variables working.
54 episodes4 hrs 45 mins

Overview

Starting completely from scratch, build a modern PHP framework with all the features you’d expect.

Whether you’re new to PHP or not, this is a great exercise for learning what happens under the hood, arming you with knowledge you can apply anywhere you use PHP.

We’ll cover routing, controllers, views, the container, accessing the database, models, authentication, config, CSRF protection, exception handling, pagination, validation, flashing messages and much more.

Let’s dive in and build a PHP framework, step-by-step!

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!