I am trying to make a MVC framework with a front controller and routing system.
The routing system works by defining paths like this...
$route->add('{controller}/{action}'); // default path
$route->add('admin/{controller}/{action}'); // admin path
To do this I am first building up a regular expression from the predefined routes.
Then I take the URL query string (e.g. home/index) and see if it matches the regular expression using preg_match();
Here is the example code...
$route = 'admin/{controller}/{action}';
$url = 'admin/controller/action';
echo $route . '<br>';
$route = preg_replace('/\//', '\\/', $route); // repaces '/' with '\/'
echo $route . '<br>';
$route = preg_replace('/{([a-z]+)}/', '(?P<\1>[a-z-]+)', $route); // for each path segment in curly {} gets replaced with a regular expression and named according to what is in the braces
echo $route . '<br>';
$route = '/^' . $route . '$/i'; // encapsulating the reg_exp
echo $route . '<br>';
preg_match($route, $url, $matches);
echo '<pre>', print_r($matches);
This outputs the following code... you can see how the regular expression is built from the path I defined... {controller}/{action}
Now the part that I simply do NOT understand is this:
?P<\1>
How is this working so that the output array keys are named after {controller} and {action}?? I cant find ANY information on this syntax and am thoroughly confused!