Note that there are some explanatory texts on larger screens.

plurals
  1. POphp url routing
    text
    copied!<p>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:</p> <pre><code>Warning: preg_match(): Empty regular expression on line 27 </code></pre> <p>This is the code:</p> <pre><code>public function buildRegex($uri, array $params) { // Find {params} in URI if(preg_match_all('/\{(?:[^\}]+)\}/', $uri, $this-&gt;matches, PREG_SET_ORDER)) { foreach($this-&gt;matches as $isMatch) { // Swap {param} with a placeholder $this-&gt;uri = str_replace($isMatch, "%s", $uri); } // Build final Regex $this-&gt;finalRegex = '/^' . preg_quote($this-&gt;uri, '/') . '$/'; $this-&gt;finalRegex = vsprintf($this-&gt;finalRegex, $params); return $this-&gt;finalRegex; } } </code></pre> <p>when i use like this:</p> <pre><code>$routeCollection-&gt;add('index', '/index.php/index/home/{name}', 'SiteName:Controller:Index', 'Home', ['name' =&gt; '(\w+)']); </code></pre> <p>it works just fine but when i don't have params and i just pass something like:</p> <pre><code>$routeCollection-&gt;add('contact', '/index.php/contact/', 'SiteName:Controller:Contact', 'index'); </code></pre> <p>i get that error. Anyway around to give me a hand fixing this problem because i'm out of ideas.</p> <p>The Entire Code of the class:</p> <pre><code>class RouterCollection { public $routeCollection = []; public function add($name, $pattern, $controller, $action = null, array $params = []) { if(!isset($this-&gt;routeCollection[$name])) $this-&gt;routeCollection[$name] = [ 'pattern' =&gt; $pattern, 'controller' =&gt; $controller, 'action' =&gt; $action, 'params' =&gt; $params, ]; } public function findMatch($url) { foreach($this-&gt;routeCollection as $routeMap) { $this-&gt;regex = $this-&gt;buildRegex($routeMap['pattern'], $routeMap['params']); // Let's test the route. if(preg_match($this-&gt;regex, $url)) { return ['controller' =&gt; $routeMap['controller'], 'action' =&gt; $routeMap['action']]; } else { return ['controller' =&gt; $this-&gt;routeCollection['404']['controller'], 'action' =&gt; $this-&gt;routeCollection['404']['action']]; } } } public function buildRegex($uri, array $params) { // Find {params} in URI if(preg_match_all('/\{(?:[^\}]+)\}/', $uri, $this-&gt;matches, PREG_SET_ORDER)) { foreach($this-&gt;matches as $isMatch) { // Swap {param} with a placeholder $this-&gt;uri = str_replace($isMatch, "%s", $uri); } // Build final Regex $this-&gt;finalRegex = '/^' . preg_quote($this-&gt;uri, '/') . '$/'; $this-&gt;finalRegex = vsprintf($this-&gt;finalRegex, $params); return $this-&gt;finalRegex; } } public function getCollection() { return $this-&gt;routeCollection; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload