Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a router in PHP?

I'm currently developing a router for one of my projects and I need to do the following:

For example, imagine we have this array of set routes:

$routes = [
    'blog/posts' => 'Path/To/Module/Blog@posts',
    'blog/view/{params} => 'Path/To/Module/Blog@view',
    'api/blog/create/{params}' => 'Path/To/Module/API/Blog@create'
];

and then if we pass this URL through: http://localhost/blog/posts it will dispatch the blog/posts route - that's fine.

Now, when it comes to the routes that require parameters, all I need is a method of implementing the ability to pass the parameters through, (i.e. http://localhost/blog/posts/param1/param2/param3 and the ability to prepend api to create http://localhost/api/blog/create/ to target API calls but I'm stumped.

like image 400
Hypernami Avatar asked Feb 23 '16 23:02

Hypernami


People also ask

What is a router in php?

Routing is what happens when an application determines which controllers and actions are executed based on the URL requested. Simply put, it is how the framework gets from http://localhost/users/list.html to the Users controller and the list() action.

How do php routes work?

In its most common configuration, PHP relies on the web server to do the routing. This is done by mapping the request path to a file: If you request www.example.org/test.php, the web server will actually look for a file named test. php in a pre-defined directory.

What is Router in MVC php?

The Router attempts to match the first one, or two, parts of the path component to a corresponding route combination ( Controller / Action [method], or just a Controller that executes a default action (method). An action, or command, is simply a method off of a specific Controller .

What is an HTML router?

A router is a JavaScript object that maps URLs to functions. The router calls a function based on the URL. In the past, a web application was a series of interconnected pages. This could either be static pages or dynamic pages that are generated on the server.


1 Answers

Here's something basic, currently routes can have a pattern, and if the application paths start with that pattern then it's a match. The rest of the path is turned into params.

<?php
class Route
{
    public $name;
    public $pattern;
    public $class;
    public $method;
    public $params;
}

class Router
{
    public $routes;

    public function __construct(array $routes)
    {
        $this->routes = $routes;
    }

    public function resolve($app_path)
    {
        $matched = false;
        foreach($this->routes as $route) {
            if(strpos($app_path, $route->pattern) === 0) {
                $matched = true;
                break;
            }
        }

        if(! $matched) throw new Exception('Could not match route.');

        $param_str = str_replace($route->pattern, '', $app_path);
        $params = explode('/', trim($param_str, '/'));
        $params = array_filter($params);

        $match = clone($route);
        $match->params = $params;

        return $match;
    }
}

class Controller
{
    public function action()
    {
        var_dump(func_get_args());
    }
}

$route = new Route;
$route->name    = 'blog-posts';
$route->pattern = '/blog/posts/';
$route->class   = 'Controller';
$route->method  = 'action';

$router = new Router(array($route));
$match  = $router->resolve('/blog/posts/foo/bar');

// Dispatch
if($match) {
    call_user_func_array(array(new $match->class, $match->method), $match->params);
}

Output:

array (size=2)
  0 => string 'foo' (length=3)
  1 => string 'bar' (length=3)
like image 136
Progrock Avatar answered Oct 25 '22 22:10

Progrock