Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP] Routing-System

pel

Skilled Illusionist
Joined
Jan 27, 2012
Messages
382
Reaction score
343
Hey Guys.

I coded a routing system to improve my skills. I didn't try to code it in OOP (because I'm a lazy fag)
How to use it:
PHP:
<?php

error_reporting(E_ALL);

require_once 'Router.php';

$router = new Router();
$router->add('/test', function() {
    echo 'You opened /test';
});
$router->add('/test/@int:id/@string:title', function($params) {
    echo 'You opened /test/'.$params['id'].'/'.$params['title'];
});


if(!isset($_GET['p'])) {
    $_GET['p'] = '/index';
}

$router->setRoute($_GET['p']);

$route = $router->checkRoutes();
if($route != 'NOT_FOUND') {
    return $route;
}

I know that the return thing is not the best solution but I'm to lazy to change it atm.

Class-Code:
PHP:
<?php

class Router {

    private $routes = [];
    private $route = '/';

    public function add($route, $handler) {
        if (!$this->routeExists($route)) {
            $this->routes[] = [$route, $handler];
        }
    }

    public function routeExists($searchingRoute) {
        foreach ($this->routes as $route) {
            if ($route[0] == $searchingRoute) {
                return true;
            }
        }

        return false;
    }

    public function setRoute($route) {
        $this->route = rtrim($route, '/');
    }

    public function checkRoutes() {

        $router = [];
        $currentRoute = 'NOT_FOUND';

        foreach ($this->routes as $route) {
            if ($route[0] == $this->route) {
                $currentRoute = $route[1]();
                return;
            }

            $explodedRoute = explode('/', $route[0]);
            $currentRoute = explode('/', $this->route);

            $foundIntegers = 0;
            $foundStrings = 0;
            $params = [];

            if (count($explodedRoute) == count($currentRoute)) {
                for ($i = 0; $i < count($explodedRoute); $i++) {
                    if (substr($explodedRoute[$i], 0, 1) == '@') {
                        $explodedArea = explode(':', $explodedRoute[$i]);
                        $type = str_replace('@', '', $explodedArea[0]);

                        if ($type == 'int' && ctype_digit($currentRoute[$i])) {
                            $params[$explodedArea[1]] = $currentRoute[$i];
                            $foundIntegers++;
                        }

                        if ($type == 'string' && !ctype_digit($currentRoute[$i])) {
                            $params[$explodedArea[1]] = $currentRoute[$i];
                            $foundStrings++;
                        }

                        if (substr_count($route[0], '@int:') == $foundIntegers && substr_count($route[0], '@string:') == $foundStrings) {
                            $currentRoute = $route[1]($params);
                            return;
                        }
                    }
                }
            }
        }
                
        return $currentRoute;
    }

}

Maybe you can post some feedback!
 
Joined
Apr 28, 2005
Messages
6,953
Reaction score
2,420
I'd look in to how CI handles their navigation. There are so many different ways to do this. It really comes down to the most simple implementation.



CI was the first framework I was able to go through and fully understand exactly what was going on. That was years ago mind you, but still.
 
Back
Top