Create your own PHP Router the simplest possible way

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

    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

    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.

    There are many requiring paid products for this purpose, but we would like to select free of charge very functional and at the same time of simple in the use editor - Codelobster PHP Edition.

    Telescope gives knowledge into the solicitations coming into your application, exemptions, log sections, database questions, lined employments, mail, notices, reserve activities, planned errands, variable dumps and that's just the beginning.

    Laravel Live Islamabad, ended on 24th Nov'18 & we got a lot to learn from the top-notch of speakers & influencers. Thanks to the sponsors for pulling off such a great event in the capital of Pakistan.