Create your own PHP Router the simplest possible way

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

    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

    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.

    Another #LaraconUS in the books & It was the 6th instalment of #LaraconUS happened at Chicago with full swing & we got a lot to learn this time with exceptional line of speakers and never-ending community support by fellow Artisans

    Laravel is one of a famous and widely used PHP framework which is growing with immense pace since its version 4.2. Learning Laravel & Setting up your application locally is not much of a difficult task but when it comes to deployment, developers often find it a bit tricky to handle.

    There are several PHP frameworks for a web development project. Among them Laravel is regarded as the best open-source PHP web framework.