Bogdan
Bogdan

Reputation: 713

php url routing

Hello i have the following code which i use to build a regex from url. the problem is when i don't pass certain params to the method i get the following error:

Warning: preg_match(): Empty regular expression on line 27

This is the code:

public function buildRegex($uri, array $params)
{
    // Find {params} in URI
    if(preg_match_all('/\{(?:[^\}]+)\}/', $uri, $this->matches, PREG_SET_ORDER))
    {
        foreach($this->matches as $isMatch)
        {
            // Swap {param} with a placeholder
            $this->uri = str_replace($isMatch, "%s", $uri);
        }
        // Build final Regex
        $this->finalRegex = '/^' . preg_quote($this->uri, '/') . '$/';
        $this->finalRegex = vsprintf($this->finalRegex, $params);

        return $this->finalRegex;
    }
}

when i use like this:

$routeCollection->add('index', '/index.php/index/home/{name}', 'SiteName:Controller:Index', 'Home', ['name' => '(\w+)']);

it works just fine but when i don't have params and i just pass something like:

$routeCollection->add('contact', '/index.php/contact/', 'SiteName:Controller:Contact', 'index');

i get that error. Anyway around to give me a hand fixing this problem because i'm out of ideas.

The Entire Code of the class:

class RouterCollection
{
    public $routeCollection = [];

    public function add($name, $pattern, $controller, $action = null, array $params = [])
    {
        if(!isset($this->routeCollection[$name]))
            $this->routeCollection[$name] =
        [
            'pattern' => $pattern,
            'controller' => $controller,
            'action' => $action,
            'params' => $params,
        ];
    }
    public function findMatch($url)
    {
        foreach($this->routeCollection as $routeMap)
        {
            $this->regex = $this->buildRegex($routeMap['pattern'], $routeMap['params']);
            // Let's test the route.
            if(preg_match($this->regex, $url))
            {
                return ['controller' => $routeMap['controller'], 'action' => $routeMap['action']];
            }
            else
            {
                return ['controller' => $this->routeCollection['404']['controller'], 'action' => $this->routeCollection['404']['action']];
            }
        }
    }
    public function buildRegex($uri, array $params)
    {
        // Find {params} in URI
        if(preg_match_all('/\{(?:[^\}]+)\}/', $uri, $this->matches, PREG_SET_ORDER))
        {
            foreach($this->matches as $isMatch)
            {
                // Swap {param} with a placeholder
                $this->uri = str_replace($isMatch, "%s", $uri);
            }
            // Build final Regex
            $this->finalRegex = '/^' . preg_quote($this->uri, '/') . '$/';
            $this->finalRegex = vsprintf($this->finalRegex, $params);

            return $this->finalRegex;
        }
    }
    public function getCollection()
    {
        return $this->routeCollection;
    }
}

Upvotes: 0

Views: 236

Answers (1)

salathe
salathe

Reputation: 51950

Your buildRegex() method returns NULL when the provided $uri has no {...} in it.

NULL is an empty regex.

The solution? Make buildRegex() always return a regex!

Upvotes: 3

Related Questions