Back to (PHP) basics. Creating your own SEO friendly “mini-website” in PHP

Created at: May 27, 2016; Last update: August 12, 2016

I want to be noted that this is not a tutorial about creating an MVC framework from zero. If you want that kind of tutorial, you can find a lot of them (https://www.youtube.com/playlist?list=PLfdtiltiRHWGXVHXX09fxXDi-DqInchFD https://www.youtube.com/playlist?list=PL7A20112CF84B2229).

There are moments in your programmer life when you are asked to do a small “landing page”, which actually turns out to be a “mini-website” with an “About” page, a “Contact” page and a few other pages.

At that moment you may simply tell yourself: “…well… we’ll simply install a framework and work with it”. But… WHY??? Why would you do such a thing for 3 or four “pages”? Why not simply create an index.php, an about.php and a contact.php?

All good and well… But the SEO specialist would say: “What about SEO friendly URL’s?”… “Oh, crap… We go back to a framework… No! A mini-framework!! No!! A micro-framework!!!”. Well… Let’s see how we can do it as they did it in the old days.

As you may well know, .htaccess file is the first one that gets “executed” when a server is receiving a request for a resource. So we first need to create a .htaccess file in the root of our… website…:

As you can see we first make sure that mod_rewrite is enabled (https://httpd.apache.org/docs/current/mod/mod_rewrite.html). After that, we must make sure that the resource selected is not a file, directory. If none of these applies, .htaccess directs the request to an index.php file appending the rest of the url to a GET type of variable named url.

Now we create the index.php which will take the variable…

Cool. We have the path. Now what? We will have to break the path into its components:

Now, we should have a default page that should deal with the request in case the user is accessing the homepage (namely, when the $path_arr is empty).

Now, how do we redirect the user to the wanted php script? First of all we must find out if that script exists. If it doesn’t, then we redirect to the homepage.

First of all… let’s put all our script files in a directory named “App”. So, let us create a file named contact.php inside that app directory:

…and a homepage.php:

Now, let’s return to our index.php and see the if-else block again:

Easy enough, right? Now, if we go to our contact page (http://localhost/contact), we will be greeted by the contact.php script. But what about eventual subpages? What if we want something like (http://localhost/contact/by-email)? For this we would need to transform the scripts into classes, and add methods to them, so that a “subpage” would actually be a method of that class.

So, going to contact.php we would need to change it into a class, and also add an “index” method:

It would be a great idea to name the file “Contact.php” (starting with uppercase). So, let’s do this also.

Let us also change “homepage.php” into “Homepage.php” and transform it into a class with an index() method:

Now returning to our index.php we need to call the script and instantiate the class.

After we instantiate the class we should call the method:

Cool… but what if we want to pass the method some other parameters inside the url?

We simply change the call by passing it the rest of our $path_arr:

Let us see the index.php again:

Now, we only need to go to app/Contact.php and see if the array is passed to our method:

That’s it. You now have the first steps toward thinking in PHP and, maybe creating a MVC (although I did say this is not about creating an MVC framework).

Entering Composer

But how about using composer in order to autoload the classes? This way we won’t need to “require_once” the files needed. I assume you already installed composer…

In order to use composer, we first need to create a composer.json file inside our root directory (where the index.php is):

Now, going in the terminal (or console) where the composer.json is, we type “composer install” which will create the vendor directory and the autoload.php file.

Once those are created, we go into our index.php and, on the first line we require the autoload.php:

Now we need to update the script so that it won’t require the files but simply instantiate the classes:

After this, we need to make Contact and Homepage classes parts of the App namespace:

This is how Contact.php looks now:

…and this is how Homepage.php looks now:

Cool. We’ve finished with the composer too.

Is all this safe???

Of course not! You would need to only leave index.php inside the public area (public_html, htdocs, www…). Move everything else in a directory above the public area, and simply change the path to the autoload.php.

3 thoughts on “Back to (PHP) basics. Creating your own SEO friendly “mini-website” in PHP

  1. steven

    i love your tutorials. i see that you have worked with ion auth in a prevous tutorial. i was wondering if you have used community auth and if so what do you think of it? Im doing a site that needs roles and permissions so i figured it would be the best for that application. if you want to do a tutorial on it also that would cool but just your professional opinions would be great. I also using thre hmvc by wiredesignz.

    Reply
    1. avenirer Post author

      Hello. I haven’t used community auth. Although it looks very professional, and I heard people being very satisfied with it, I haven’t yet felt the need to use it. I did use hmvc in the past, but now, when I look at it, I often find myself lost inside all the directories that I created then, and all the calls to other modules. I ended up being the adept of “don’t complicate yourself when you don’t have to”. You don’t need to use modules (libraries) that are too complex in order to do simple things (as a result you can see this tutorial, which is simply telling you that you don’t need a framework to create basic stuff). Programmers tend to overcomplicate things, considering that if it’s not complicated then it doesn’t worth their attention. So, my advice is: Make sure you really need those modules or libraries you think you need, before implementing them. 🙂 I won’t do a tutorial about hmvc as I don’t really use it anymore (I am not in the habit of doing tutorials about things I do not use – this site is not a link baiter).

      Reply
  2. Federico

    Lovely and useful tutorial…as usual your post are a great inspiration.
    Thanks again for all your efforts and post

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.