dannyvankooten / AltoRouter

PHP routing class. Lightweight yet flexible. Supports REST, dynamic and reversed routing.
https://dannyvankooten.github.io/AltoRouter/
MIT License
1.22k stars 230 forks source link

Bug in setting base path #157

Closed mtfash closed 8 years ago

mtfash commented 8 years ago

Hi there,

This is my scenario. The following is the directory structure for my project, which is accessible via http://localhost/phpprac:

D:/wamp/www/phpprac/
|_ vendor/
|_ .htaccess
|_ index.php

And this is the index.php:

<?php
require 'vendor/autoload.php';

$router = new AltoRouter ();
$router->setBasePath('/phpprac/');
// map homepage
$router->map ( 'GET', '/', 'some_action', 'some_name');

$match = $router->match ();
var_dump($match);

And this is the output:

boolean false

If I set the base path with a slash at the end, this will cause the match() function always returning false. For example the base path $router->setBasePath('/some-base-path/') causes match() method to return false always. The root cause for this is in the value returned by substr() function in AltoRouter/AltoRouter.php line 179:

// strip base path from request url
$requestUrl = substr($requestUrl, strlen($this->basePath));

Here substr() returns false instead of / for a root url index. Although for other urls It will remove the slash at the beginning of the request url. So in this situation the match will always return false.

However If I remove the slash from the end of base path, it works.

$router->setBasePath('/some-base-path')

koenpunt commented 8 years ago

Thanks, but this is not really a bug, because if we would strip the slash of the end, setting the base path to some-base-path/ wouldn't work anymore.