Browse Source

better basePath support

Maurits van der Schee 5 years ago
parent
commit
31845e1a6a
2 changed files with 22 additions and 1 deletions
  1. 1
    0
      api.php
  2. 21
    1
      src/Tqdev/PhpCrudApi/Middleware/Router/SimpleRouter.php

+ 1
- 0
api.php View File

@@ -7858,6 +7858,7 @@ $config = new Config([
7858 7858
     'username' => 'php-crud-api',
7859 7859
     'password' => 'php-crud-api',
7860 7860
     'database' => 'php-crud-api',
7861
+    'debug' => false,
7861 7862
 ]);
7862 7863
 $request = RequestFactory::fromGlobals();
7863 7864
 $api = new Api($config);

+ 21
- 1
src/Tqdev/PhpCrudApi/Middleware/Router/SimpleRouter.php View File

@@ -25,7 +25,7 @@ class SimpleRouter implements Router
25 25
 
26 26
     public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl, bool $debug)
27 27
     {
28
-        $this->basePath = $basePath;
28
+        $this->basePath = $this->detectBasePath($basePath);
29 29
         $this->responder = $responder;
30 30
         $this->cache = $cache;
31 31
         $this->ttl = $ttl;
@@ -36,6 +36,21 @@ class SimpleRouter implements Router
36 36
         $this->middlewares = array();
37 37
     }
38 38
 
39
+    private function detectBasePath(string $basePath): string
40
+    {
41
+        if ($basePath) {
42
+            return $basePath;
43
+        }
44
+        if (isset($_SERVER['PATH_INFO'])) {
45
+            $fullPath = array_shift(explode('?',$_SERVER['REQUEST_URI']));
46
+            $path = $_SERVER['PATH_INFO'];
47
+            if (substr($fullPath, -1*strlen($path)) == $path) {
48
+                return substr($fullPath, 0, -1*strlen($path));
49
+            }
50
+        }
51
+        return '';
52
+    }
53
+
39 54
     private function loadPathTree(): PathTree
40 55
     {
41 56
         $data = $this->cache->get('PathTree');
@@ -101,6 +116,11 @@ class SimpleRouter implements Router
101 116
         return $request;
102 117
     }
103 118
 
119
+    public function getBasePath(): string
120
+    {
121
+        return $this->basePath;
122
+    }
123
+
104 124
     public function handle(ServerRequestInterface $request): ResponseInterface
105 125
     {
106 126
         $request = $this->removeBasePath($request);

Loading…
Cancel
Save