Api Platform conference
Register now
v2.1 Operation Path Naming
API Platform Conference
September 19-20, 2024 | Lille & online

The international conference on the API Platform Framework

API Platform Conference 2024: meet the best PHP, JavaScript and API experts

Learn more about the event, register for the conference, and get ready for two days of inspiration, ideas, and knowledge-sharing with our incredible lineup of renowned specialists and advocates.

Register now

# Operation Path Naming

With API Platform Core, you can configure the default resolver used to generate operation paths. Pre-registered resolvers are available and can easily be overridden.

# Configuration

There are two pre-registered operation path naming services:

Service nameEntity namePath result
api_platform.path_segment_name_generator.underscoreMyResource/my_resources
api_platform.path_segment_name_generator.dashMyResource/my-resources

The default resolver is api_platform.path_segment_name_generator.underscore. To change it to the dash resolver, add the following lines to app/config/config.yml:

# app/config/config.yml

api_platform:
    path_segment_name_generator: api_platform.path_segment_name_generator.dash

# Create a Custom Operation Path Resolver

Let’s assume we need URLs without separators (e.g. api.tld/myresources)

# Defining the Operation Path Resolver

Make sure the custom resolver implements ApiPlatform\Core\PathResolver\OperationPathResolverInterface:

<?php

// src/AppBundle/PathResolver/NoSeparatorsOperationPathResolver.php

namespace AppBundle\PathResolver;

use ApiPlatform\Core\PathResolver\OperationPathResolverInterface;
use Doctrine\Common\Inflector\Inflector;

final class NoSeparatorsOperationPathResolver implements OperationPathResolverInterface
{
    public function resolveOperationPath(string $resourceShortName, array $operation, bool $collection) : string
    {
        $path = Inflector::pluralize(strtolower($resourceShortName));
        if (!$collection) {
            $path .= '/{id}';
        }
        $path .= '.{_format}';

        return $path;
    }
}

Note that $resourceShortName contains a camel case string, by default the resource class name (e.g. MyResource).

# Registering the Service

If you haven’t disabled the autowiring option, the service will be registered automatically and you have nothing more to do. Otherwise, you must register this class a service like in the following example:

# app/config/services.yml
services:
    'AppBundle\PathResolver\NoSeparatorsOperationPathResolver': ~

# Configure It

# app/config/config.yml

api_platform:
    path_segment_name_generator: 'AppBundle\PathResolver\NoSeparatorsOperationPathResolver'

You can also help us improve the documentation of this page.

Made with love by

Les-Tilleuls.coop can help you design and develop your APIs and web projects, and train your teams in API Platform, Symfony, Next.js, Kubernetes and a wide range of other technologies.

Learn more

Copyright © 2023 Kévin Dunglas

Sponsored by Les-Tilleuls.coop