Banner of None

Creating A Real Website with PHP Object Oriented part 5 URL Handling, Views and website behavior


Category: php

Date: August 2020
Views: 1.74K


We will decide how our website will Handle the URLS, to comply with Google recommendations for building the urls, we will not use GET parameters, instead we will build our own logic to handle the parts of our URLS.
First we will list all possible urls that our website will have and then decide how to handle them. I decided that my website will only respond to these URLS:

https://mywebsite.com/
https://mywebsite.com/Wallpapers
https://mywebsite.com/Wallpapers/All
https://mywebsite.com/Wallpapers/All/1
https://mywebsite.com/Wallpapers/Full/1
https://mywebsite.com/Wallpapers/nature/1
https://mywebsite.com/Wallpapers/nature/1

the only relevant parts of our urls are the green and red parts, the "All" and "Full" are just keywords to identify whether we are browsing all Wallpapers in our database or showing a single wallpaper. any other word or words will be considered a search query. like "nature". The red part of our urls should always be numbers, they will represent the page number or in the case of "Full" the number will be the wallpaper id.


Simple enough we just gonna have to do a simple switch on the green part and check if the red part exists and it is a number. But first lets create a class that will handle viewing the html code, since at this point we are dealing with things that are not database related. the following class will be considered like an abstract class and the parent of all other "Views" classes that we will create for our website:




class Views {
    protected const ONE = 1;
    protected const ALL = 2;
    protected const SEARCH = 3;
    protected $toview;
    protected $db;
    protected function one(){}
    protected function all(){}
    protected function search(){}
    public function view(){
        switch($this->toview){
            case self::ONE:
                $this->one();
                break;
            case self::ALL:
                $this->all();
                break;
            case self::SEARCH:
                $this->search();
                break;
        }
    }
}


The three constants ONE, All and SEARCH will be used to decide what we are viewing, ie which function we should call, the 3 functions that we declared and not defined just yet one(), all(), and search().
The Only public function we created is view(). the code is clear enough to understand that the member variable toview will decide which function to call depending on its value. the other member variable db will be an instance to the Wallpapers Class we created in the previous tutorial.


The next class will inherit our "Views" Class . it is the class the will be doing the viewing:




class ViewWallpapers extends Views {
    private $id = -1;
    private $searchquery;
    private $perPage = 24;
    private $first;
    public function __construct(){

        $urlparts = array_slice(explode("/",$_SERVER["REQUEST_URI"]),2);

        //here goes the logic to handle the parts of our URLS and assign
        //values to our class member variables accordingly
    }
    protected function one(){
        echo "viewing the wallpaper with id $this->id";
    }
    protected function all(){
        echo "showing all wallpapers starting from $this->first";
    }
    protected function search(){
        echo "showing wallpapers tagged $this->searchquery starting from $this->first";
    }
}


In the ViewWallpapers Class we define the $id that will hold the id of a single Wallpaper that will be viewed by one() function. the $searchquery will be used to search() the wallpapers. the 3 functions are inherited from "Views" and defined here. later they will be used to print the html code of our website.
The constructor function will get the URL parts and store the relevant parts that we talked about in the beginning of this tutorial. using the REQUEST_URI from the $_SERVER variable.
the next lines of code will be added to the __construct function above and everything will fall into place:




public function __construct(){
    $urlparts = array_slice(explode("/",$_SERVER["REQUEST_URI"]),2);
    if(empty($urlparts[0])){
        $this->toview = self::ALL;
        $this->first = 0;
    }else{
        switch($urlparts[0]){
            case "All":
                $this->toview = self::ALL;
                if(!empty($urlparts[1]) && ctype_digit($urlparts[1])) $this->first = $urlparts[1];
                else $this->first = 0;
                break;
            case "Full":
                if(!empty($urlparts[1]) && ctype_digit($urlparts[1])){
                    $this->id = $urlparts[1];
                    $this->toview = self::ONE;
                }
                break;
            default:
                $this->toview = self::SEARCH;
                if(!empty($urlparts[1]) && ctype_digit($urlparts[1])) $this->first = $urlparts[1];
                else $this->first = 0;
                $this->searchquery = urldecode($urlparts[0]);
                break;
        }
    }
    $this->db = new Wallpapers();
}


with this we will be ready to start adding some html to our code. the next video will illustrate this tutorial as always. see you in the next tutorial.



1.74K views

Previous Article Next Article

2 Comments, latest

  • Nerrylaunc
    9 months ago

    Hello from Happykiddi.

  • admin Admin Reply
    9 months ago

    Nerrylaunc:
    Hello from Happykiddi.

    Hello Happykiddi, I wonder if this old article was helpful to you.

Reply to admin: Cancel