In a full web application, you’ll be working with a lot of separate — but well-organized — files.
It’s best to use different files to establish a separation of concerns: each file contains code that performs a distinct piece of functionality.
So it’s good to start practicing this now.
Move the MySQL connection code to a separate file
For example, in your full web application, your database connection code will live in a separate file.
But right now, your MySQL code is contained in your controller file. To complete this exercise, you’re going to separate it out into its own file.
There are just two simple steps:
- Move all of the MySQL code it to its own file, and name the file accordingly (e.g.
includethat file from within your
And remember: after you make the changes, your page should look exactly the same, because you didn’t change any content, you just changed the code’s organization.
You will continue to see (and have already seen) this mantra of optimizing your code’s organization pop up over and over again. Organization is key, when building a web app. Great organization is crucial to ensure that:
- Your code is designed as efficiently as possible.
- It’s easy to build upon and expand your application, moving forward.