Skip to main content
Version: Latest
FeatureAIv1.4.3open-dxp/headless-bundle

Naming Conventions

An endpoint is made of four classes, and each one has a job. The name should say which job it does, so that a reader who opens the folder can tell them apart without reading any code.

src/Domain/Product/
Query/ProductsQuery.php what the caller wants
Action/ListProductsAction.php how it arrives over HTTP
QueryHandler/ProductsQueryHandler.php where it is answered
ResponseEntity/ProductItem.php what goes back

Messages​

A query is named after what it returns. Use the plural for a collection and the singular for one record.

ProductsQuery      // a list of products
ProductQuery // one product
OrderHistoryQuery // one caller's order history

A command is named after the change it makes. Write it as an instruction, verb first, and keep it in the present tense.

CreateAddressCommand
UnsubscribeCommand
ApplyVoucherCommand
Tip

Read the class name out loud as a sentence. CreateAddressCommand says "create address", which is an instruction. AddressCreateCommand says nothing, and that is the sign that the words are the wrong way round.

Actions​

The action is the HTTP side of the endpoint. It carries the operation, while the message carries the intent, so the two names are not the same word twice.

For a query, the operation follows the resource:

GetProductAction    // returns one record
ListProductsAction // returns a collection

For a command, the message name already begins with a verb, so the action repeats it and adds nothing else:

CreateAddressCommand  ->  CreateAddressAction
UnsubscribeCommand -> UnsubscribeAction
Note

Get and List are not interchangeable. Get answers with a single record, List answers with a collection. Anyone reading the class list can therefore see which endpoints paginate.

Some operations are neither a plain read nor a change to one resource. Name those with their own verb and a noun, and avoid the five words above so that the exception stays visible:

PickupCartAction
ApplyVoucherToCartAction

Handlers and response entities​

A handler is named after the message it answers, with Handler appended. There is one handler per message, so nothing else needs to be decided.

ProductsQuery  ->  ProductsQueryHandler

A response entity is named after the shape it describes, not after the endpoint that returns it. The same entity often serves several endpoints.

ProductItem
ProductSearchItem
EcommerceProduct
Tip

When a response entity overrides one from a bundle, give it the same short name as its parent. The namespace already tells the two apart, and the matching name makes the pair obvious in response_entity_mapping. See Response Entities.

Filters, transformers and source resolvers​

Each of these is named after what it does, with its role appended:

PaginationFilter
DistanceInKmTransformer
OpeningHoursSourceResolver

The makers follow these rules​

Both makers derive every name from the two arguments you pass, so a generated endpoint already matches the conventions above:

bin/console make:api-query Product Products --collection
# ProductsQuery, ListProductsAction, ProductsQueryHandler, ProductsItem

bin/console make:api-command Address CreateAddress
# CreateAddressCommand, CreateAddressAction, CreateAddressCommandHandler, CreateAddressItem

For an operation whose name cannot be derived, pass it yourself:

bin/console make:api-command Cart ApplyVoucher --action-name=ApplyVoucherToCart

See Development Tools.