Develop Simple PHP MVC framework

Pointing all the requests to a central location

.htaccess file can be used to do that. Here is an example where all the requests to a certain website is pointed to index.php in the root directory.

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^([^?]*)$ index.php?url=$1 [NC,L,QSA]

place the .htaccess file in your root directory and create a file index.php in the same directory with some dummy content. And access the site using different urls like

your-domail.com/
your-domail.com/about/us
your-domail.com/product/234/view

You will still see that all the requests to the website goes directly to the index.php

Process all URL’s from a single file

Processing all the requests to the website using a single file/function is required. Routes.php is used in this example.

<?php
class Router{
    function __construct(){
        if(isset($_GET['url'])){
            $raw_url = $_GET['url'];
            $urlTrim = rtrim($raw_url, "/");
            /** making an array of parts of url ( seperated by / ) */
            $url = explode("/", $urlTrim);
            $controller_name = $url[0];
            if(file_exists(ABSPATH."Controller/".$controller_name.".php")){
                /**
                 * if controller file is there, mc-autoload.php will load it
                 * here the controller is actually created/instantiated
                 */
                $controller = new $controller_name();
                /**
                 * if 2 or more string phrases available in the url,
                 * pass 2nd and later strings as parameters to the action
                 */
                if(isset($url[2])){
                    $action = $url[1];
                    /**
                     * removing 0th and 1st elements from $url array
                     */
                    array_shift($url);
                    array_shift($url);
                    $data = $url;

                    /**
                     * check whether action function exists
                     */

                    if(method_exists($controller, $action)){
                        $controller->{$action}($data);
                    } else {
                        $controller = new Error();
                    }
                } else {
                    if(isset($url[1])){

                        /**
                         * check whether the action function exists
                         */
                        if(method_exists($controller, $url[1])){
                            /**
                             * calling the action with no parameters
                             */
                            $controller->{$url[1]}();
                        } else {
                            $controller = new Error();
                        }
                    } else {
                        /**
                         * calling to home page, if there is no action specified
                         */
                        $controller = new Error();
                    }
                }
            } else {
                /**
                 * If controller file is not found, the error/404 page is loading
                 */
                require_once (ABSPATH."Controller/error.php");
                $controller = new Error();
                return false;
            }
        } else {
            /** if url/controller is not set, load home page */
            require_once(ABSPATH.'Controller/index.php');
            $controller = new Index();
        }
    }
}