Hello guys, I'm Johnny
and today we are going to configure our local and production environment.
First of all before diving into some code
let's explain a little bit what is the importance of having different configuration on your local and your production environment.
First of all the local and the production environment is always different. From major
configurations such as the database password
to minor ones such as showing some extra logging.
Creating configuration files is one of the most boring tasks
but believe me they will be really beneficial for the long run as you will have a well-structured project!
And of course for security reasons. I hope you don't have the same password locally with your production environment
Do you?
And lastly, clean code and clean git repositories as you don't need to hardcode
environmental configurations everywhere within your code anymore!
Now, let's see how we can change those environmental configurations.
The good news is that CodeIgniter is using a well known technique for that.
CodeIgniter is using a .env file to get the environment configuration
`.env` is a simple and a very powerful tool as it is meant to have only environmental files.
Ok, no more waiting! We will now dive into some code and let's see an example at your Codeigniter project.
You will notice that there is a file with name `env`
You can copy this file and then rename it to `.env`
with the dot at the beginning. If you are using Windows
you can simply rename it to `.env.`
and it will magically rename it to you to `.env`
Now open the file with your favorite editor.
Usually the
configurations that we are changing are the following: The local URL is different from the one that we have on the production.
For example a conversion that I'm using is replacing the
`www` with `local` so I will not forget the local URL name! For example if
`www.grocerycrud.com` is my production URL, my local URL would be
`local.grocerycrud.com` so I can easily remember it. As you can also see from my screen I'm commenting out the variable
`app.baseURL` and adding my local URL path.
Oh, and by the way,
this is the way that we will add environment variables, by commenting out our desired configurations.
The second most common variable that we change
is our database credentials. There is not much to talk about database credentials to be honest. Search for the word
`database` and then uncomment the `database.default` credentials with yours as you can also see at your screen. And of course
don't forget to uncomment the most important variable with name `CI_ENVIRONMENT`
If you are at your local environment, you should have the `CI_ENVIRONMENT` equals
`development` as you can also see at your screen
Once you have done that you will notice two main differences at your website. First, you will notice the debug tool at the bottom right.
The debug tool is giving you some useful information about the page
and this will also help you at the next tutorials as you can see some detailed information
about the page. For example at this page
I can see that I'm using the Controller `Webpages` and that my view path is the one that you can see at your screen.
If you don't know what the Controller and the View is don't worry as this will probably be my very next tutorial.
So if you don't want to miss that don't forget to subscribe to my channel. Also an
important note here about the debug tool. In case you can't see that the debug bar - and it is driving you crazy -
don't worry, as you are not alone. You've probably haven't configured `baseURL` correctly.
For example, I've noticed while I was trying to present you the `baseURL`
that I couldn't see the debug bar.
Once I've changed it to the actual `baseURL` of the project that the debug bar was there again! The second main
difference that you will notice when you are in development mode is a more detailed error reporting. As a math nerd
I like to try dividing a number with zero that I know that so far
It will return me an error. As you can also see at your screen that is a detailed error reporting about the issue.
However, if I change my environment into `production`, you will see that the error page is just a generic error page
that doesn't give you ANY information about the error. So let's sum up the most important things that we've learned today:
`.env` files are different in your local environment and in production environments. For example in my local machine the `.env` file looks like this!
Although in production environment, it looks like this!
Number two `.env` file shouldn't be in your (git) codebase as these are
sensitive data. That simply means that you have to add it to the .gitignore
file - if this is not already there. In my personal experience
it is very important to start with environment files first to make sure that we have everything we need before we start.
That's it guys! I hope you did enjoy the video. I wanted to keep this video short so it can be useful and informative
Also, I am very excited as I have just released Grocery CRUD for Codeigniter 4.
Thank you again for supporting this channel.
Stay safe and speak to you guys to the next video.
Bye!