Routing without .htaccess
When you use the flight script as part of the URL there is currently no way to let flight automatically adjust the routing logic. (Setting the base_url had no effect)
My URLs look like: http://localhost:8000/backend.php/profile as example for the /profile route.
As a work-around I used:
$scriptName = $_SERVER['SCRIPT_NAME'];
$request = Flight::request();
$request->url = substr($request->url, strlen($scriptName));
it would be great if flight could support that use case as well.
I do the same sometimes, I use this
Flight::before('start', function () {
Flight::request()->url = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/';
});
PATH_INFO is the part of the URL after /index.php or what have you - the SCRIPT_NAME - but before the query string begins. Although one benefit with your approach is that the query string will be preserved, assuming your REQUEST_URI includes it. Mine usually don't for whatever reason, but it's still present in QUERY_STRING. CGI is a strange beast.
It would be nice if Flight would support this without mod_rewrite. But detecting mod_rewrite is not easy (or impossible if the shell_exec function is disabled) on CGI. Without CGI it's as simple as running in_array('mod_rewrite', apache_get_modules()); (Courtesy of http://stackoverflow.com/a/9021523/1353158). How do you think it would be implemented?
I would think it could be done by just adding a method that would enable that. (being disabled by default)
Flight::useRouter(true);
Or something similiar like that.
That sounds good to me. There is no need to automatically detect mod_rewrite since it is configured manually anyway. You could set an environment variable along with the mod_rewrite configuration that could be detected by flight, as complement to programmatic configuring the routing logic.
I'm going to try to and make Flight be able to run without URL rewriting in the next build.
有的服务器不支持路由映射。目前折中的如此解决:
Flight::before("start", array("MyHelper", "withoutRoute")); Flight::start();
final class MyHelper { public static function withoutRoute() { Flight::route('GET|POST /*', array(CLASS, 'routeDispense')); }
public static function routeDispense()
{
if (isset($_GET['ctrl'])) {
$class = ucwords(trim($_GET['ctrl'])) . "Controller";
} else {
$class = "HomeController";
}
if ($class === 'Controller') {
$class = 'HomeController';
}
$method = isset($_GET['act']) ? trim($_GET['act']) : "main";
if (empty($method)) {
$method = 'main';
}
if (class_exists($class) && method_exists($class, $method)) {
call_user_func(array(new $class(), $method));
} else {
Flight::halt(500, 'class or method not found: ' . $class . '::' . $method);
}
}
}
Duplicate of #205