diff --git a/src/RestModelConfigProviderAbstract.php b/src/RestModelConfigProviderAbstract.php index a852255..676cacb 100644 --- a/src/RestModelConfigProviderAbstract.php +++ b/src/RestModelConfigProviderAbstract.php @@ -34,7 +34,6 @@ public function __invoke() { return [ 'routes' => $this->routeGroup([ - 'path' => $this->pathPrefix, 'middleware' => $this->getMiddleware(), ], $this->getRoutes() @@ -97,6 +96,8 @@ protected function createRoute( $handler = $middleware; } + $path = $this->pathPrefix . '/' . ltrim($path, '/'); + $route = [ 'name' => $name, 'path' => $path, @@ -150,8 +151,15 @@ protected function createModelRoute( if ($handler === null) { $handler = $this->defaultHandler; } + + $trimmedEndpoint = trim($endpoint, '/'); + $endpointPrefix = trim($this->pathPrefix, '/'); + $fullEndpoint = $endpointPrefix . '/' . $trimmedEndpoint; + $trimmedFullEndpoint = strtolower(trim($fullEndpoint, '/')); + $cleanEndpointName = strtolower(str_replace(['/'], '.', trim($trimmedFullEndpoint, '/'))); + if ($privilege === null) { - $privilege = "pr.api.$endpoint"; + $privilege = "pr.$cleanEndpointName"; } $routeParameters = '/[{' . $idField . ':' . $idRegex . '}]'; @@ -177,14 +185,20 @@ protected function createModelRoute( $settings = array_merge($settings, $options); } + $privilegeLabel = $trimmedFullEndpoint; + if (str_starts_with($privilegeLabel, 'api/')) { + $privilegeLabel = substr($privilegeLabel, 4); + } + $routes = []; if (!empty($methods)) { - $name = "api.$endpoint.structure"; + $name = "$cleanEndpointName.structure"; $settings['privilege'] = $privilege . '.structure'; + $settings['privilegeLabel'] = "API: $privilegeLabel -> /structure"; $routes[$name] = [ 'name' => $name, - 'path' => '/' . $endpoint . '/structure', + 'path' => '/' . $trimmedFullEndpoint . '/structure', 'middleware' => $handler, 'options' => $settings, 'allowed_methods' => ['GET'] @@ -193,15 +207,15 @@ protected function createModelRoute( foreach($methods as $method) { $path = match ($method) { - 'GET' => '/' . $endpoint . '['.$routeParameters.']', - 'POST' => '/' . $endpoint, - 'PATCH', 'PUT', 'DELETE' => '/' . $endpoint . $routeParameters, + 'GET' => '/' . $trimmedEndpoint . '['.$routeParameters.']', + 'POST' => '/' . $trimmedEndpoint, + 'PATCH', 'PUT', 'DELETE' => '/' . $trimmedEndpoint . $routeParameters, default => null, }; - $name = "api.$endpoint.$method"; + $name = "$cleanEndpointName.$method"; $settings['privilege'] = "$privilege.$method"; - $settings['privilegeLabel'] = "API: $endpoint -> $method"; + $settings['privilegeLabel'] = "API: $privilegeLabel -> $method"; [$methodRoute] = $this->createRoute( name: $name, @@ -209,6 +223,7 @@ protected function createModelRoute( handler: $handler, allowedMethods: [$method], options: $settings, + privilege: $settings['privilege'], ); $routes[$name] = $methodRoute; }