0

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;
    }
}
2
  • 1
    Show us your add method. Commented Jan 24, 2013 at 8:03
  • well i'll post the entire code ok ? Commented Jan 24, 2013 at 8:08

1 Answer 1

3

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.