0

Good day everyone. I have the following two functions one for adding a rule and the other one for matching that rule. The problem is that when i use two params one of them doesn't get changed and i don't understand why it doesn't work. any help is apreciated.

public function add($name, $pattern, $controller, $action = null, array $params = array())
{
    if(!isset($this->routeCollection[$name]))
        $this->routeCollection[$name] =
    array(
        '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 array('controller' => $routeMap['controller'], 'action' => $routeMap['action']);
        }
    }

    return array('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);
        var_dump($this->finalRegex);
    } else {
        $this->finalRegex = '/^(' . preg_quote($uri, '/') . ')$/';
        $this->finalRegex = str_replace(array('\.', '\-'), array('.', '-'), $this->finalRegex);
    }

    return $this->finalRegex;
}

// Usage:

$routeCollection->add('CatalogByCategory', '/catalog/category/{slugLink}', 'Ex:Controller:Catalog', 'ViewByCategory',
    array('slugLink' => ('[a-z0-9]+(?:-[a-z0-9]+)*') ));
$routeCollection->add('ListCatalogPageByCategory', '/catalog/category/{sluglinks}/{pageNumber}', 'Ex:Controller:Catalog', 'ListCatalog',
    array('sluglinks' => ('[a-z0-9]+(?:-[a-z0-9]+)*'), 'pageNumber' => ('[1-9][0-9]*') ));

// From Dump:

string '/^\/catalog\/category\/[a-z0-9]+(?:-[a-z0-9]+)*$/' (length=49)
string '/^\/catalog\/category\/\{sluglinks\}\/[a-z0-9]+(?:-[a-z0-9]+)*$/' (length=64)
HamZa
  • 14,671
  • 11
  • 54
  • 75
Bogdan
  • 693
  • 7
  • 26
  • 2
    Please locate the concrete location where the issue arises. This works normally best with a step-debugger. Sometimes using `strtr` instead of `str_replace` is the solution already, but you posted too much code for my taste to look deeper on my own. - More information: [When to use strtr vs str_replace?](http://stackoverflow.com/q/8177296/367456) – hakre Apr 11 '14 at 11:55
  • the problem is in buildRegex function, it doesn't change two params in a url structure only 1. – Bogdan Apr 11 '14 at 12:22
  • I'd say then that Niet the Dark Absol's answer is then the better hint. It's perhaps easier if you use [`preg_replace`](http://php.net/preg_replace) as an alternative way to do replacements. Also you should to proper error checking, there is a difference between a wrong regex or an overlfow returning FALSE and just a zero-length match. Also debug: Use at least `var_dump` to verify the returned matches are in the format you're looking for them. Normally the first match is the whole string. – hakre Apr 11 '14 at 12:34
  • yes indeed. :-) thank you everyone for taking time. – Bogdan Apr 11 '14 at 12:37

1 Answers1

1
    foreach ($this->matches as $isMatch) {
        // Swap {param} with a placeholder
        $this->uri = str_replace($isMatch, "%s", $uri);
    }

You keep overwriting $this->uri with the value of $uri being run through a replacement - in this case, it's getting set with {sluglinks} being replaced, then set again with only {pageNumber} being replaced.

You should use $this->uri = $uri;, and then always use $this->uri.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592