[eluser]Mario Z.[/eluser]
Hi all,
First, my congratulations for this good and neat framework !
I have been testing it for a while, and I am beginning a new project with it.
But I am stucked with the (recurring) URI routing problem.
I have read some previous threads about this, and about why the segmented URLs are best, and not using query strings, etc.
But
the problem is I have an existing and defined API, which has been used in a lot of Javascript code, and what I want is to reimplement it using CodeIgniter, without changing the REST API. Not too crazy, ok ? That's the way things should be done, change the implementation, without changing the interface. Old OO practice.
It has been in good use for a long time, has a good REST design, etc. So changing it is not possible.
The API uses a lot of query strings and segmented URLS too, Something like document/id=1234/body?as=pdf&draft=yes&sign=xxxx
I tried developing a router, which will receive the request, transform it into a valid CodeIgniter route, and then call the corresponding controller-method.
I extracted some code from the Router.php, and created a standard draft RestRouter.php script to act as a front end to CodeIgniter (the code is below).
My problem is I canĀ“t imagine how to call the CodeIgniter controller from this script.
Using redirect (changing the http location header) seems like a bad solution (it will go back to the browser and then again to the server).
And I don't want to touch the CodeIgniter core (I know some routing solutions do this).
I will rather use an external Adapter without messing with the CodeIgniter core.
Can somebody help me here ? :-S
Thanks in advance !!!
The draft RestRouter.php code
=============================
Code:
// Configure the Router mappings here
$routes['GET']["/sem/rest.php/document/id=(:any)/preview?as=(:any)&draft;=(:any)&sign;=(:any)"]
="/sem/newApi.php/document/preview/$1/$2/$3/$4";
// URL processing here
$url = $_SERVER['REQUEST_URI'];
$method = $_SERVER['REQUEST_METHOD'];
if (($method == "POST") && ($_POST['_method']=="PUT")) { $method = "PUT"; }
if (($method == "POST") && ($_POST['_method']=="DELETE")) { $method = "PUT"; }
$redirect=router($routes[$method], $url) ;
echo "<h3>url:$url</h3>";
echo "<h3>Redirect:$redirect</h3>";
//http_redirect($redirect); ???
// Convert the URL
// this has been taken from Router.php
function router($routes, $uri) {
// Loop through the route array looking for wild-cards
foreach ($routes as $key => $val)
{
// Convert wild-cards to RegEx
$key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
// To support QUERY_STRING, we must escape '&' and '?'
$key = str_replace('?', '\?', str_replace('&', '\&', $key));
echo "<br/>uri:$uri<br/>key:$key";
// Does the RegEx match?
if (preg_match('#^'.$key.'$#', $uri))
{
// Do we have a back-reference?
if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
{
$val = preg_replace('#^'.$key.'$#', $val, $uri);
}
return $val;
}
}
}