Creating a real website with PHP object oriented - part 1 the site layout and htaccess

tutorial, php, programming,

For this tutorial, I assume that you have a basic knowledge of php and classes and objects, the programming language doesn't matter, for the principle of classes and object is the same.

What will be the key point of this tutorial is to show you how to implement these abstract notions in a real life website using a real database.



Before firing up your text editor to start coding you have to take your time to envision the idea of your website , you have to imagine it online and how you expect your users/clients/visitors to interact with it. you have to imagine every possible scenario. of course you can't be expected to have a detailed idea about it, but even a general idea will help.

Our website in this tutorial will have a single index.php file and we will make sure all the website traffic will go through our index.php file. later on we will make sure to handle every possible link in our website in a neat and precise manner. but lets cross that bridge when we get to it.

Our website layout

we will follow an MVC Model to create our website. this model's aim is to divide the responsibilities of our php files : the M stands for the Model, its all the php classes that will handle the logic, the database queries, and the algorithms, they will be separated from the classes that will be responsible for creating the html objects ( buttons, images, forms ....) these php classes are the V part of the MVC Model : the views. The C in the MVC Model is the Controllers ; these are the classes that will make changes to our database, they will be responsible for the insertions, updates and deletions.

Lets make our website

Part 1 : the website directories

First we create our website directories in the following tree structure:

    css
    js
    images
    resources
        classes
        includes
        views
    index.php
    .htaccess

.htaccess and changing the server rules and behavior

As I said before, we need to envision how the website will work beforehand and before we create it. What I envisioned for my website is that I want all the traffic to go through a single file: index.php. I want to deny direct access to the directories and I want to redirect my website users to my index.php whenever they try to access a directory or a file that does not exist. Luckily the magic file .htaccess do just that and so many more. it basically alters the rules of the webserver.

the following instructions will make all the conditions I mentioned above:


Options -Indexes
ErrorDocument 403 /index.php
ErrorDocument 404 /index.php

the first line will deny direct access to existing directories in our server by disabling directories indexing, this will result in a 403 error access denied when trying to access a directory in our server, this is when the second line of our .htaccess file kicks in: the 403 access denied will be redirected to the index.php file and our visitor is none the wiser. the third line is a redirection from 404 file not found pages to index.php as well.

In the next tutorial we will make a database connection using mysql PDO, stay tuned. In the mean time this is a video that illustrates this tutorial.

Buy me A Coffee

Comments:

: We will never share your email