I am just experimenting with custom php routing, the following code works:
class Router
{
public function run()
{
$routes = [
'dog/indexadmin' => '\\Mini\\Controller\\DogController@indexAdmin',
'dog/index' => '\\Mini\\Controller\\DogController@index',
'dog/edit' => '\\Mini\\Controller\\DogController@edit'
];
$uri = explode('/', $_SERVER['REQUEST_URI']);
$last = array();
foreach ($uri as $i => $key) {
$i > 0;
$key;
if ($i > 3) {
$newkey = strtok($key, '?');
array_push($last, $newkey);
}
}
$totalparts = $i;
$lastkey = $newkey;
if ($totalparts > 3) {
$str = implode(", ", $last);
}
$uri = $uri[2] . '/' . $uri[3];
$newuri = strtok($uri, '?');
$key_to_check = $newuri;
if (array_key_exists($key_to_check, $routes)) {
$uparts = explode('@', $routes[$key_to_check]);
$controller = isset($uparts[0]) ? $uparts[0] : null;
$action = isset($uparts[1]) ? $uparts[1] : null;
call_user_func_array(array($controller, $action), $last);
}
}
}
Namely the
call_user_func_array(array($controller, $action), $last);
$last is just uri parameter parts.
The url is something like:
http://localhost/mini3/dog/indexadmin/hello/jims
However, if I wanted to use
if ($totalparts > 3) {
$c = new $controller;
$c->$action($str);
} else {
$c = new $controller;
$c->$action();
}
Instead of call_user_func_array
If there are no extra parameters like:
http://localhost/mini3/dog/indexadmin
it works. However if there are the extra parameters
/hello/jim
It does not work. My function:
$str = implode(", ", $last);
properly comma separates them for this part:
$c->$action($str);
But just passes all as one string.
If I hard code in
$c->$action("hello", 'jim");
it works.
How would I get 2 or more parameters like “hello”, 'jim" into one variable
to pass to controller, but controller treats them as comma separated individual strings?
In controller I have:
public function indexAdmin($tmp = null, $name = null)
{
echo $tmp . " " . $name;