User 18473564 User 18473564 - 17 days ago 6
PHP Question

Custom routing doesn't work correctly [F3]

The custom route doesn't work correctly and always routes to user.htm

index.php

$routes = [
"/" => "index.htm",
"/user/id=@id" => "user.htm","/user/@id" => "user.htm",
];

foreach ($routes as $path => $file) {
$f3->route("GET ".$path,
function($f3){
global $file,$path;
echo View::instance()->render($file);
}
);
}

Answer

try this:

$routes = [
  "/" => "index.htm",
  "/user/id=@id" => "user.htm",
  "/user/@id" => "user.htm",
];

foreach ($routes as $path => $file)
{
  $f3->route("GET " . $path,
    function ($f3) use ($file)
    {
      echo View::instance()->render($file);
    }
  );
}
Comments