Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(operation-path-naming): compatibility with laravel #2088

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
];
```
Loading