Explicit application actions in Laravel Go
Use the language's ordinary boundaries
Extract one business operation into an app-owned Go function or service, called by the app's existing handler, job, listener, or command. At the time of writing, the Package Port catalog marked a Go port as not applicable because applications can use plain reusable functions; this is not a claim that every PHP action feature is built in. Preserve behavior rather than translating traits, magic static methods, or decorator discovery. Verify it in a real application with the trial below.
The source has multiple contracts
The PHP package's object path resolves from the container and invokes handle. Controller decoration may authorize/validate and choose JSON/HTML response hooks. The queued path can invoke asJob instead of handle, serialize models, and apply queue/retry/failure settings. Command/listener paths can choose their own adapters. Inventory all of them before assuming a single method is the whole action.
Direct run does not automatically run controller authorization/rules. Attribute-validation traits can add their own validation path. Skipped runIf returns a PHP Fluent, not the operation's normal result. In Go, choose an explicit result/error or skipped outcome; do not return an ambiguous zero-valued result as success.
Implementation plan
- Inventory every caller and hook: traits, constructor dependencies,
handle/as*methods, authorization, rules/validation preprocessing, response mapping, listener registration, queue connection/name/timeouts/retries, uniqueness/debounce, and fakes. Scope this extraction to one concrete operation. Keep specialized queue features on their existing path until separately designed. - Put the use case beside the application's existing actions/services, following local conventions. Accept
context.Context, explicit trusted actor/tenant identity, and a typed input. Return an application result anderror. Use existing dependencies or a service receiver when it has state; a new generic action dispatcher is unnecessary. - Keep parsing and HTTP response rendering in the handler, but enforce domain invariants and authorization at the shared use-case boundary. CLI/queue calls must not bypass policy because they lack an HTTP request. Define a trusted system actor explicitly where needed. Reject foreign ownership from the shared action, and recheck mutable state inside the transaction.
- Use
db.Transaction(ctx, func(txctx context.Context) error { ... })with a context carrying the application's connection. Every query/write in the callback usestxctx, not the outer request context. Return errors to trigger rollback. Do not launch a goroutine that outlives the callback or move transaction-scoped dependencies to global state. - Let adapters map the result to a response, exit status, or job retry. Do not put an HTTP response writer or request object in the shared input. Async jobs carry stable serializable identifiers and an explicit actor/tenant identity, then reload current records and permissions at execution. Preserve missing-model and changed-authorization behavior intentionally.
- Preserve queue/timeouts/backoff/failure semantics in the native job adapter. At-least-once execution requires domain idempotency, not a belief that a unique job runs exactly once. Use the existing durable operation-key/outbox strategy for side effects.
db.AfterCommitruns after a successful commit; callback failure is not a rollback of committed data. Keep old job handlers available while already-enqueued PHP payloads drain or are migrated with an approved plan. - Replace package-specific fake assertions with tests that exercise the actual function and public adapters. Queue fakes prove dispatch decisions, not database rollback, authorization, or external idempotency.
Illustrative division, not runnable declarations or invented Go framework methods:
handler: parse + authenticate → use case(ctx, actor, input) → resource/error mapping
job: load IDs + actor → use case(ctx, actor, input) → retry/failure handling
command: parse + authorize → use case(ctx, actor, input) → output/exit status
Trial acceptance
Use an existing tenant-owned project rename or similarly bounded operation, through only the entry points the real app uses.
- HTTP and direct calls produce the same business result; invalid fields and wrong tenants change nothing. HTTP status/error bodies and CLI exit codes match their original contracts.
- A queued operation rechecks current ownership and actor authorization, including a revocation after enqueue.
- Inject a failure after the first database write and read from a separate connection: no partial write commits, exposing accidental use of the outer context.
- Deliver the same job twice and verify the chosen durable idempotency behavior. A cancelled context stops work according to the operation's documented commit boundary.
- Exercise missing/deleted records, timeouts, configured queues, failure hooks, and old queued payload compatibility.
- No mutable request/tenant state survives into another worker request or concurrent invocation.
Run gofmt, go vet ./..., go test ./..., go test -race ./..., plus production-database and real queue-driver integration checks where the operation uses them. Record actual versions, adapters tested, and deliberate differences with the change.
Sources
- MIT reference: lorisleiva/laravel-actions 2.13.0,
Concerns/AsObject.php,AsJob.php,ValidateActions.php, controller/job decorators, andtests/AsObjectTest.php,AsCommandAndObjectTest.php,AsControllerWithAuthorizeAndRulesTest.php, and job tests. PreserveLICENSE.mdfor adapted source. - Laravel Go baseline, module
go.laravel.com, especiallydb/tx.gofor transaction contexts andAfterCommitbehavior. Match the app's locked revision. - Port options: check
get-recipe's current port catalog (sourced from Package Port). A not-applicable classification is a design recommendation, not package-API parity.