core icon indicating copy to clipboard operation
core copied to clipboard

Routing without .htaccess

Open everflux opened this issue 11 years ago • 5 comments

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.

everflux avatar Dec 06 '14 01:12 everflux

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.

kafene avatar Dec 06 '14 04:12 kafene

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.

ghost avatar Apr 08 '15 13:04 ghost

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.

everflux avatar Apr 10 '15 10:04 everflux

I'm going to try to and make Flight be able to run without URL rewriting in the next build.

mikecao avatar Apr 14 '15 19:04 mikecao

有的服务器不支持路由映射。目前折中的如此解决:

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);
    }
}

}

gzu-liyujiang avatar Apr 30 '15 21:04 gzu-liyujiang

Duplicate of #205

n0nag0n avatar Jan 03 '24 21:01 n0nag0n