Skip to content

Commit

Permalink
docs(operation-path-naming): compatibility with laravel
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceAmstoutz committed Dec 6, 2024
1 parent 4461006 commit ff2b37b
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions core/operation-path-naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ Pre-registered resolvers are available and can easily be overridden.
There are two pre-registered operation path naming services:

| Service name | Entity name | Path result |
| -------------------------------------------------------------- | ------------ | --------------- |
|----------------------------------------------------------------|--------------|-----------------|
| `api_platform.metadata.path_segment_name_generator.underscore` | `MyResource` | `/my_resources` |
| `api_platform.metadata.path_segment_name_generator.dash` | `MyResource` | `/my-resources` |

The default resolver is `api_platform.metadata.path_segment_name_generator.underscore`.

### Configuration using Symfony

To change it to the dash resolver, add the following lines to `api/config/packages/api_platform.yaml`:

```yaml
Expand All @@ -21,6 +24,19 @@ api_platform:
path_segment_name_generator: api_platform.metadata.path_segment_name_generator.dash
```
### Configuration using Laravel
To change it to the dash resolver, add the following lines to `config/api-platform.php`:

```php
<?php
// config/api-platform.php
return [
// ....
'path_segment_name_generator' => 'api_platform.metadata.path_segment_name_generator.dash'
];
```

## Create a Custom Operation Path Resolver

Let's assume we need URLs without separators (e.g. `api.tld/myresources`)
Expand All @@ -31,7 +47,7 @@ Make sure the custom segment generator implements [`ApiPlatform\Metadata\Operati

```php
<?php
// api/src/Operation/SingularPathSegmentNameGenerator.php
// api/src/Operation/SingularPathSegmentNameGenerator.php with Symfony or app/Operation/SingularPathSegmentNameGenerator.php with Laravel
namespace App\Operation;
use ApiPlatform\Metadata\Operation\PathSegmentNameGeneratorInterface;
Expand Down Expand Up @@ -61,7 +77,7 @@ class SingularPathSegmentNameGenerator implements PathSegmentNameGeneratorInterf

Note that `$name` contains a camelCase string, by default the resource class name (e.g. `MyResource`).

### Registering the Service
### Registering the Service (for Symfony only)

If you haven't disabled the autowiring option, the service will be registered automatically and you have nothing more to
do.
Expand All @@ -74,10 +90,23 @@ services:
'App\Operation\SingularPathSegmentNameGenerator': ~
```

### Configuring It
### Configuring the Service

#### Configuring It using Symfony

```yaml
# api/config/packages/api_platform.yaml
api_platform:
path_segment_name_generator: 'App\Operation\SingularPathSegmentNameGenerator'
```

#### Configuring It using Laravel

```php
<?php
// config/api-platform.php
return [
// ....
'path_segment_name_generator' => App\Operation\SingularPathSegmentNameGenerator::class
];
```

0 comments on commit ff2b37b

Please sign in to comment.