Explicit application actions in Laravel Rust
Replace one use case, not PHP's decorators
Move one package-backed business operation into an app-owned Rust function or service that existing HTTP, job, listener, and command adapters call explicitly. Keep transport-specific response mapping outside the operation. Exclude generic decorator discovery, universal run helpers, automatic fakes, dynamic action routing, unique/debounced jobs, and full package parity. This is source-based guidance; verify it with the trial checks below.
At the time of writing, the Package Port catalog listed no direct Rust equivalent and noted a partial action package. Check get-recipe's current port catalog and offer compatible partial ports if the actual caller needs their capabilities. A plain function is often the smallest correct answer when the app needs only one reusable operation.
Understand the different execution paths
In the reference, AsObject::run resolves an object from Laravel's container and calls handle. Controller decoration can run authorization/validation and map the result through JSON/HTML response hooks. Job decoration invokes asJob if present, otherwise handle, and carries queue configuration/model serialization. Commands/listeners can call different adapter methods. Those paths are not interchangeable.
In particular, direct run does not automatically execute the controller's authorization/rules. WithAttributes/validation traits add separate behavior that must be inventoried. Conditional execution returns an empty PHP Fluent when skipped, which should become an explicit Rust result/option contract rather than a pretend model. A fake or queue assertion is not evidence the real business operation was authorized or committed correctly.
Application-owned boundary
- Inventory the selected action's traits, constructor dependencies,
handle,asController,asJob,asListener,asCommand, validation/authorization hooks, response transformers, job metadata, and fake assertions. Search every caller before deleting a trait or static dispatch call. Preserve existing public routes, command names, listener registration, and payload compatibility through explicit adapters. - Choose a typed command input and domain result/error. Pass trusted actor/tenant identity separately from untrusted request fields. Resolve dependencies through the app's existing container or constructor pattern; do not introduce a global static service locator to imitate PHP syntax.
- Normalize/validate transport data at the boundary, but enforce business invariants and authorization inside the use case or a shared policy it always calls. A queued job or command has no browser session. Explicitly define any system-actor permission; absence of a request is not authorization. Recheck mutable ownership/state within the transaction where races matter.
- Keep related database writes on the same connection in the installed transaction callback. Laravel Rust scopes same-connection queries within that callback; a spawned task can escape its lifetime and must not be used for transactional work. Domain errors propagate as errors, not success responses containing an error string.
- Keep HTTP adapters responsible for status codes/resources, command adapters for output/exit status, and job adapters for retry/failure semantics. A job carries stable serializable identifiers, then reloads current records and actor/tenant permissions. Do not serialize a request, transaction, open handle, or service object. Preserve behavior for deleted models rather than assuming it matches PHP model restoration.
- Match queue names, timeouts, retry/backoff, and failure handling explicitly. For non-idempotent side effects, define an operation key with durable conflict handling; queue uniqueness is not exactly-once delivery. Schedule side effects after commit or use the app's existing outbox. After-commit dispatch failure cannot roll back an already committed write; do not blindly retry the entire operation. Retain the old queue adapter while legacy payloads drain.
- Replace package fakes with tests of observable results and boundary behavior. Use the framework's existing test facilities; do not recreate action-decorator classes solely to keep old assertion method names.
Example division of responsibility, not runnable framework API:
HTTP handler → validated command + authenticated actor → use case → domain result → HTTP resource
Job handler → persisted IDs + explicit actor identity → same use case → retry/failure policy
CLI command → parsed arguments + authorized actor → same use case → exit status
Trial checks
Pick an operation such as renaming a tenant-owned project. Run it through its real HTTP, direct, queue, and CLI entry points that actually exist in the app; do not invent adapters just for this recipe.
- Authorized valid input changes the intended project and returns the same domain result. Foreign-tenant and missing actor cases leave state unchanged on every entry point.
- Invalid/blank/oversized names fail through the documented validation contract. HTTP error mapping and CLI nonzero exit codes remain correct.
- An injected second-write failure rolls back the first from a fresh database connection; no success notification escapes the rollback.
- Retry/deliver the same queued operation twice and verify the declared idempotency behavior. Authorization revoked between enqueue and execution is re-evaluated.
- Deleted models, job timeouts/failures, existing payloads, and configured queues behave as explicitly documented.
- Dependency discovery finds no remaining calls to package dispatch/fakes for this operation before removal.
Run cargo fmt --all --check, strict Clippy, and public action/route/job/command tests on the actual stack. Record framework versions, queue driver, and deliberate differences with the change; these steps are acceptance criteria, not claimed completed tests.
Sources
- MIT reference: lorisleiva/laravel-actions 2.13.0,
Concerns/AsObject.php,AsAction.php,AsJob.php,ValidateActions.php,Decorators/ControllerDecorator.php,JobDecorator.php, and object/controller/job/command tests. RetainLICENSE.mdwhen adapting source. - Laravel Rust 0.1.3 baseline; check actual transaction, queue, command, and dependency-injection APIs in the app's lockfile.
- Port options: check
get-recipe's current port catalog (sourced from Package Port); partial port listings are not full parity claims.