Search code examples
php.htaccessxamppklein-mvc

Klein url router not working on XAMPP


My app is on C:\xampp\htdocs\urlrouter\klein\

I installed the klein router using composer.
And, I use this script just for simple basic routing

define('APP_PATH', '/urlrouter/klein/');
require_once 'vendor/autoload.php';
$request = \Klein\Request::createFromGlobals();
$request->server()->set('REQUEST_URI', substr($_SERVER['REQUEST_URI'],  strlen(APP_PATH)));
$klein = new \Klein\Klein();
$klein->respond('GET', '/hello', function () {
return 'Hello World!';
});
$klein->dispatch($request);

And I also have this .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L] 

But, when I go to http://localhost/urlrouter/klein/hello , it redirects me to the XAMPP homepage or http://localhost/xampp/splash.php
I can't figure out what's wrong with this router. Please help me


Solution

  • I've never tried to manipulate the REQUEST_URI with Klein (not saying you shouldn't, just warning of a lack of expertise), but your substr() call is going to yield "hello", where your route pattern is "/hello". That might or might not matter (the route matching logic in Klein is somewhat complex, and I haven't internalized all its details). At any rate, I think it's worth a try to define your APP_PATH as '/urlrouter/klein' instead of '/urlrouter/klein/'.

    If that works, cool. If not, post a comment and I'll try to reproduce what you're seeing.