bd-better-route-bridge
better-data: Composing with better-route
For developers using better-data and better-route together — DTO-backed REST endpoints, OpenAPI generation from DTO schemas, request hydration into typed DataObject instances inside route handlers. The integration seam is the optional BetterRouteBridge (src/Route/BetterRouteBridge.php); using it correctly keeps the data layer free of router concerns and the router layer free of data-shape concerns.
Misconception this skill corrects
"I'll just use
register_rest_routedirectly inside my better-data consumer code, parseWP_REST_Requestmyself, and callMyDto::fromArray($request->get_params())."
That works for one route. For an API of 10+ routes, it duplicates the request-parsing, validation, route-owned-field, and Presenter-projection wiring at every callsite. The bridge centralizes that pipeline:
- Register the route on
better-route'sRoutervia the appropriate HTTP-verb method. - On request, hydrate a
WP_REST_Request-shaped object into the DTO (URL params, JSON body, query string — buckets resolved persourceoption). - Reject collisions: a route-owned field like
id(in the URL/posts/{id}) MUST NOT also appear in the JSON body —RequestParamCollisionException(line 194, 680). - Validate the DTO via the
BuiltInValidator. - Call the handler with
(DataObject $dto, mixed $request). - If the handler returns a
DataObject, present throughPresenter::for($dto)->context(PresentationContext::rest()).
Other AI-prone misconceptions: