php-router: Wrong route when run in sub-folder

Hello. I just check your code and got one issue when run with sub-folder

Example URL: http://localhost/cms/test cms is sub-folder

$router->get('test','Test@test');

Return Route


array(7) {
--
["router"]=>
  array(7) {
    ["route"]=>
    string(5) "/cms/test"
    ["method"]=>
    string(3) "GET"
    ["callback"]=>
    string(9) "Test@test"
    ["name"]=>
    string(9) "test.test"
    ["before"]=>
    NULL
    ["after"]=>
    NULL
    ["group"]=>
    NULL
  }

route should be only test

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 23 (14 by maintainers)

Commits related to this issue

Most upvoted comments

I’ve just released a new version that contains some fixes related to the issue. You can update your package via composer!

@peter279k It make sense. Actually, I’ve started to work to add sub-folder support on php-router. I’m continue to work. We will figure it out.

This is my folder. image

index.php

<?php
require_once(__DIR__.'/vendor/autoload.php');
$router = new Buki\Router([
    'base_folder' => __DIR__
]);
$router->get('/',function() use ($router) {
    var_dump($router);
    return 'Hello World!';
});
$router->get('/test',function() use ($router) {
    var_dump($router);
    return 'Hello World!';
});
$router->run();

this is result

object(Buki\Router)#3 (10) {
  ["baseFolder":protected]=>
  string(20) "D:\Xampp\htdocs\demo"
  ["routes":protected]=>
  array(2) {
    [0]=>
    array(7) {
      ["route"]=>
      string(6) "/demo/"
      ["method"]=>
      string(3) "GET"
      ["callback"]=>
      object(Closure)#2 (1) {
        ["static"]=>
        array(1) {
          ["router"]=>
          *RECURSION*
        }
      }
      ["name"]=>
      NULL
      ["before"]=>
      NULL
      ["after"]=>
      NULL
      ["group"]=>
      NULL
    }
    [1]=>
    array(7) {
      ["route"]=>
      string(10) "/demo/test"
      ["method"]=>
      string(3) "GET"
      ["callback"]=>
      object(Closure)#4 (1) {
        ["static"]=>
        array(1) {
          ["router"]=>
          *RECURSION*
        }
      }
      ["name"]=>
      NULL
      ["before"]=>
      NULL
      ["after"]=>
      NULL
      ["group"]=>
      NULL
    }
  }
  ["groups":protected]=>
  array(0) {
  }
  ["patterns":protected]=>
  array(12) {
    ["{a}"]=>
    string(7) "([^/]+)"
    ["{d}"]=>
    string(5) "(\d+)"
    ["{i}"]=>
    string(5) "(\d+)"
    ["{s}"]=>
    string(5) "(\w+)"
    ["{u}"]=>
    string(10) "([\w\-_]+)"
    ["{*}"]=>
    string(4) "(.*)"
    [":id"]=>
    string(5) "(\d+)"
    [":number"]=>
    string(5) "(\d+)"
    [":any"]=>
    string(7) "([^/]+)"
    [":all"]=>
    string(4) "(.*)"
    [":string"]=>
    string(5) "(\w+)"
    [":slug"]=>
    string(10) "([\w\-_]+)"
  }
  ["namespaces":protected]=>
  array(2) {
    ["middlewares"]=>
    string(0) ""
    ["controllers"]=>
    string(0) ""
  }
  ["paths":protected]=>
  array(2) {
    ["controllers"]=>
    string(11) "Controllers"
    ["middlewares"]=>
    string(11) "Middlewares"
  }
  ["mainMethod":protected]=>
  string(4) "main"
  ["cacheFile":protected]=>
  bool(false)
  ["cacheLoaded":protected]=>
  bool(false)
  ["errorCallback":protected]=>
  NULL
}

You can see demo included routes lists