@@ -4478,6 +4478,7 @@ public function route(ServerRequestInterface $request): ResponseInterface;
4478
4478
4479
4479
class SimpleRouter implements Router
4480
4480
{
4481
+ private $ basePath ;
4481
4482
private $ responder ;
4482
4483
private $ cache ;
4483
4484
private $ ttl ;
@@ -4487,8 +4488,9 @@ class SimpleRouter implements Router
4487
4488
private $ routeHandlers ;
4488
4489
private $ middlewares ;
4489
4490
4490
- public function __construct (Responder $ responder , Cache $ cache , int $ ttl , bool $ debug )
4491
+ public function __construct (string $ basePath , Responder $ responder , Cache $ cache , int $ ttl , bool $ debug )
4491
4492
{
4493
+ $ this ->basePath = $ basePath ;
4492
4494
$ this ->responder = $ responder ;
4493
4495
$ this ->cache = $ cache ;
4494
4496
$ this ->ttl = $ ttl ;
@@ -4548,8 +4550,26 @@ private function getRouteNumbers(ServerRequestInterface $request): array
4548
4550
return $ this ->routes ->match ($ path );
4549
4551
}
4550
4552
4553
+ private function removeBasePath (ServerRequestInterface $ request ): ServerRequestInterface
4554
+ {
4555
+ if ($ this ->basePath ) {
4556
+ $ path = $ request ->getUri ()->getPath ();
4557
+ $ basePath = rtrim ($ this ->basePath , '/ ' );
4558
+ if (substr ($ path , 0 , strlen ($ basePath )) == $ basePath ) {
4559
+ $ path = substr ($ path , strlen ($ basePath ));
4560
+ $ request = $ request ->withUri ($ request ->getUri ()->withPath ($ path ));
4561
+ }
4562
+ } elseif (isset ($ _SERVER ['PATH_INFO ' ])) {
4563
+ $ path = $ _SERVER ['PATH_INFO ' ];
4564
+ $ request = $ request ->withUri ($ request ->getUri ()->withPath ($ path ));
4565
+ }
4566
+ return $ request ;
4567
+ }
4568
+
4551
4569
public function handle (ServerRequestInterface $ request ): ResponseInterface
4552
4570
{
4571
+ $ request = $ this ->removeBasePath ($ request );
4572
+
4553
4573
if (count ($ this ->middlewares )) {
4554
4574
$ handler = array_pop ($ this ->middlewares );
4555
4575
return $ handler ->process ($ request , $ this );
@@ -6948,7 +6968,7 @@ public function __construct(Config $config)
6948
6968
$ cache = CacheFactory::create ($ config );
6949
6969
$ reflection = new ReflectionService ($ db , $ cache , $ config ->getCacheTime ());
6950
6970
$ responder = new Responder ();
6951
- $ router = new SimpleRouter ($ responder , $ cache , $ config ->getCacheTime (), $ config ->getDebug ());
6971
+ $ router = new SimpleRouter ($ config -> getBasePath (), $ responder , $ cache , $ config ->getCacheTime (), $ config ->getDebug ());
6952
6972
foreach ($ config ->getMiddlewares () as $ middleware => $ properties ) {
6953
6973
switch ($ middleware ) {
6954
6974
case 'cors ' :
@@ -7048,6 +7068,7 @@ class Config
7048
7068
'cachePath ' => '' ,
7049
7069
'cacheTime ' => 10 ,
7050
7070
'debug ' => false ,
7071
+ 'basePath ' => '' ,
7051
7072
'openApiBase ' => '{"info":{"title":"PHP-CRUD-API","version":"1.0.0"}} ' ,
7052
7073
];
7053
7074
@@ -7184,6 +7205,11 @@ public function getDebug(): bool
7184
7205
return $ this ->values ['debug ' ];
7185
7206
}
7186
7207
7208
+ public function getBasePath (): string
7209
+ {
7210
+ return $ this ->values ['basePath ' ];
7211
+ }
7212
+
7187
7213
public function getOpenApiBase (): array
7188
7214
{
7189
7215
return json_decode ($ this ->values ['openApiBase ' ], true );
@@ -7280,11 +7306,7 @@ public static function getParams(ServerRequestInterface $request): array
7280
7306
7281
7307
public static function getPathSegment (ServerRequestInterface $ request , int $ part ): string
7282
7308
{
7283
- if (isset ($ _SERVER ['PATH_INFO ' ])) {
7284
- $ path = $ _SERVER ['PATH_INFO ' ];
7285
- } else {
7286
- $ path = $ request ->getUri ()->getPath ();
7287
- }
7309
+ $ path = $ request ->getUri ()->getPath ();
7288
7310
$ pathSegments = explode ('/ ' , rtrim ($ path , '/ ' ));
7289
7311
if ($ part < 0 || $ part >= count ($ pathSegments )) {
7290
7312
return '' ;
0 commit comments