Tags

, , , , ,

To get Books of Sanjib Sinha…

To know more about the Author….

The concept of this dynamic website is not very critical. Usually when we type any URL like http://sanjibsinha.com/about.php, we get a response. The ‘about.php’ page opens up. We could send some data dynamically to that page whatsoever. However, this URL is not considered to be search engine friendly. It would be definitely good if the URL was something like this: http://sanjibsinha.com/about.php?query=about-sanjib-sinha. It has more clarity and the search bots will find it friendly to index.

OOP-PHP

Our challenge is, can we make something like this?
If someone types or click a link such as shown above, the ‘about.php’ page will automatically open up in the background.
You can always do it procedurally, but our challenge is different. We want to make it in Object Oriented Way.

I’m a Full-Time writer. If you find these articles helpful please consider buying any of my books. You can buy from either Leanpub or Amazon or from my publisher Apress.

Let us think about a ‘page’ object that will redirect us to a new URL from the entry point. The ‘index.php’ page of the application could be a good candidate for being the entry point. So, we will write this code on the top of the ‘index.php’ page.

require 'vendor/autoload.php';

$page = new Sanjib\PageRequest();
$page->redirect();
/*
* This is our entry point
*/

After that, we need some links from this page so that our ‘page’ object should redirect us to the desired URL. Here is the code so that we could go to ‘about.inc.php’ and ‘contact.inc.php’.

  • About
  • Contact

  • We have created a directory called ‘Google’. We have created a directory called ‘lib’. In our ‘composer.json’ file we have written this code so that any file saved in the ‘lib’ folder can be accessed through the namespace ‘Sanjib’.
    {
    "autoload": {
    "psr-4": {
    "Acme\\": "library/",
    "Sanjib\\": "lib/",
    "Myapp\\": "mylib/"
    }
    }
    }

    After setting our composer file, we run the necessary commands in our respective terminals. Whether you are using Windows, Linux or MAC, go to the application folder, and type – ‘composer dump-autoload’. This will set your namespace.
    In the ‘lib’ folder, we have kept our ‘page’ object class – ‘PageRequest.php’.
    Here is the code.

    namespace Sanjib;

    class PageRequest {

    protected $pageDirectory = "Google";
    protected $pageFolder = [];
    protected $SanjibSinha;

    function __construct() {

    }

    function redirect() {

    if(!empty($_REQUEST['Google'])){

    $$this->SanjibSinha = $_REQUEST['Google'];

    $$this->pageFolder = scandir($$this->pageDirectory, 0);
    unset($$this->pageFolder[0], $$this->pageFolder[1]);

    if(in_array($$this->SanjibSinha . ".inc.php", $$this->pageFolder)){

    require $$this->pageDirectory . "/" . $$this->SanjibSinha . ".inc.php";

    } else {

    echo 'Page not found.';

    }
    } else {
    return FALSE;
    }

    }

    }

    The logic is simple. If the page request is not empty, then the ‘request’ property will be pointed to the directory we mentioned earlier. Here it is ‘Google’. You can name your directory. It’s your choice.
    Open your ‘about.inc.php’ page and write this code:


    if ($_REQUEST['Google'] != "author-technologist-sanjib-sinha"){
    header("Location: http://localhost/dashboard/test/Google/bad.inc.php");
    }

    It assures that if someone wants to type something else it’ll automatically redirect it to a ‘Bad Request’ page. The ‘contact.inc.php’ page code will be similar.

    if ($_REQUEST['Google'] != "contact-sanjib-sinha"){
    header("Location: http://localhost/dashboard/test/Google/bad.inc.php");
    }


    Now you can create as many pages as you wish and you can set the URL dynamically. Your ‘page’ object will handle the whole request cycle now.

    Advertisements