Skip to content

Document single action controllers - #75

Open
filipebraida wants to merge 1 commit into
adonisjs:mainfrom
filipebraida:docs/single-action-controllers
Open

Document single action controllers#75
filipebraida wants to merge 1 commit into
adonisjs:mainfrom
filipebraida:docs/single-action-controllers

Conversation

@filipebraida

Copy link
Copy Markdown

The v6 docs covered single action controllers, but the section was not carried over to v7. This adds it back to the controllers guide, adapted to the v7 barrel file syntax.

What is covered

  • The handle method convention, with a complete controller example.
  • Binding the controller to a route by passing it in an array with no method name.
  • The auto-generated route name.
  • A dependency injection example, since single action controllers are ordinary controllers.
  • A note on naming them after the operation rather than a resource.

Beyond the v6 content

Two details that were not in the v6 docs:

Scaffolding the controller. make:controller accepts action names as positional arguments, so node ace make:controller register_newsletter_subscription handle --singular generates the class with handle already in place via actions.stub.

The --singular flag is required here. Without it, generators.controllerName() calls .plural() and produces RegisterNewsletterSubscriptionsController, which defeats the purpose of a single action controller. This felt worth calling out explicitly rather than leaving as a surprise.

Placement

The section sits between "Dependency injection" and "Resource-driven controllers", so the two controller design patterns read as a pair. The guide's intro bullet list was updated to match.

Verification

  • Behaviour checked against @adonisjs/http-server@9.2.0, where the array form resolves handler[1] ?? 'handle', and the route name is the snake_cased controller name with no .handle segment appended.
  • Generator behaviour checked against @adonisjs/core@7.4.0 (commands/make/controller.js, make/controller/actions.stub) and @adonisjs/application@9.0.1 (controllerName/controllerFileName).
  • Rendered locally with node ace serve --hmr: the [!code ++] markers highlight the four intended lines, the tip callout renders, and the #makecontroller cross-reference resolves to an existing anchor.
  • prettier --check and npm run lint pass.

Covers the `handle` method convention, binding the controller to a route
without a method name, and the auto-generated route name.

Also documents that `make:controller` needs the `--singular` flag for this
case, since the generator pluralizes controller names by default.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant