We were just able to remove lots of required statements by adding an artful loader.
Another place where we're repeating ourselves is with the database in several scripts we're creating
a database object and then calling the get com method on that object to get the database connection.
However we don't want to put this in the init script which we're calling on every page as we don't always
want to connect to the database unless a farm has been submitted like here in the luggin script and
in some scripts like the logout script we don't need the database connection at all.
So instead let's copy this code and create a new file in the includes folder called dbe up HP and place
this code in there as we saw earlier though.
We don't want to create a variable in an include file that were later going to use in a script that
we include this file in as our coalwood will be difficult to follow.
What we can do though is return a value from a required file when it's loaded using the require include
statement just like we do in a function.
So instead of assigning this connection object to a variable Let's return it like that.
Then in the index page we can replace these two lines with it require of the new file we just created.
And as we returning the connection object from this require we can assign it to a variable like that.
And when we look at the index page in the browser it still works as before.
Let's do the same in the other scripts where we're using the database first.
In the article script.
The edit articles script
and the delete articles script.
Then in the new article script we don't need the database connection until the farm is submitted.
So the database connection lines we need to replace our inside this big block
and it's the same for the logon script here.
Let's make sure everything still works.
Let's log in.
Then we'll create a new article.
We'll edit it.
Delete it.
And finally lock out everything is working as before but now we've removed some repeated code by creating
the database connection in a separate file.
We'll see later on.
How's this helps us when deploying our code to a live web server.