samedi 7 février 2015

Matching a request path against a route in PHP


Vote count:

0




I'm building my own routing class purely for the sake of learning. I'm trying to figure out how to match a request path against a route that has dynamic URI segments. For example, matching the following:



$regex = [
'/{(.+?):num}/' => '{$1:[0-9]+}',
// ...
];

$route = '/users/{id:num}/';
$route = preg_replace(array_keys($regex), array_values($regex), $route);

$request = '/users/5';


Once I replace the human-friendly rule with the actual regular expression how would I compare the strings?


I was thinking of using the explode function to separate each string into arrays of segments and using the route array as expressions to match the request array against but I'm not exactly sure if this would be practical or the best solution.



asked 49 secs ago

Nick

56






Matching a request path against a route in PHP

Aucun commentaire:

Enregistrer un commentaire