Create your own PHP Router the simplest possible way

Last Updated at : September 25, 2018 By - Azeem Hassni | 2434

    Introduction

    Inspired from Laravel’s router, lets build our very own PHP’s router for managing our endpoints well for our next PHP project.

    Setting up

    Let’s make a new directory for our demo project & create couple of files we’re going to be using i.e. index.php & router.php.

    Basic flow would be like, in our index.php, we’ll setup our routes and endpoints & simply passing the data in the callbacks.

    In our router.php, paste the following code.

    <?php
    
    /**
     * Holds the registered routes
     *
     * @var array $routes
     */
    $routes = [];
    
    /**
     * Register a new route
     *
     * @param $action string
     * @param \Closure $callback Called when current URL matches provided action
     */
    function route($action, Closure $callback)
    {
        global $routes;
        $action = trim($action, '/');
        $routes[$action] = $callback;
    }
    
    /**
     * Dispatch the router
     *
     * @param $action string
     */
    function dispatch($action)
    {
        global $routes;
        $action = trim($action, '/');
        $callback = $routes[$action];
    
        echo call_user_func($callback);
    }

    As you can see, there are two main functions here, one is route(), which accepts an action & a callback. This route() method will determine the action will return the provided callback. The other function is dispatch() and it will only accept an action.

    Now inside of our index.php, we can simply call like.

    <?php
    
    require_once "router.php";
    
    route('/', function () {
        return "Hello World";
    });
    
    route('/about', function () {
        return "Hello form the about route";
    });
    
    $action = $_SERVER['REQUEST_URI'];
    dispatch($action);

    Video Tutorial

     

    Github Repo

    https://github.com/azeemhassni/simplest-php-router

    blog user
    Azeem Hassni

    I write code and i love it. I do write blogs on things which I am passionate about. I love to Explore the internet of things and whats new in web technologies. My topic of Interests include WordPress, Laravel & Magento to name a few.

    Related Posts

    Importing Excel files come handy when you’re building some large scale inventory kind of an application where you need to import set of data from CSV or excel files. In this short tutorial, we’ll go through on importing excel files with Laravel & look on for some crazy hacks and tricks in between.

    Checkout how to install xDebug with pecl on your MacOSX

    Gear up fellow developers, another Laravel Meetup is coming back to Lahore & it will be a great milestone while helping our fellow web developers across the city to become motivated and will help them grow as Laravel developers.

    Getting an app developed is not the only thing that one needs to ensure in terms of digital visibility and e-commerce. Yes, having an app is important but to stay updated and to be abreast with the competition Optimization of performance is necessary.