Laravel PHP · Inline✓ Human-reviewed

App-owned application actions in Laravel PHP

Replace a package action with an ordinary injected class and native adapters without losing controller hooks, shared authorization, job payload compatibility, or idempotency.

Approved 2026-12-04T13:20:40.76747Z · Submission 56

App-owned application actions in Laravel PHP

Keep one business operation and explicit adapters

Replace one laravel-actions action with an ordinary application service/action class plus the controllers, jobs, listeners, and Artisan commands the app actually needs. Retain the existing business behavior and public interfaces. Do not build a trait/decorator compatibility layer or recreate all static helpers, fakes, routing discovery, unique/debounced jobs, and macros. This is guidance, not an executed application migration.

First separate the package's entry points

At the source baseline, AsObject::run resolves an instance from the container and calls handle. It does not automatically invoke controller authorization/rules. Controller decoration can authorize/validate, select asController, and map results through jsonResponse/htmlResponse. Attribute-validation traits are a distinct path. Job decoration prefers asJob when present, otherwise handle, and carries model serialization and job configuration. Commands and listeners can also have their own adapters.

Conditional runIf/runUnless return an empty Fluent when skipped, not a normal domain result. Direct invocation, controller invocation, and queued execution must not be conflated when replacing those helpers.

Smallest app-owned replacement

  1. Inventory the selected class's traits, constructor dependencies, every handle/as* method, validation preprocessing and hooks, authorization, response transforms, job metadata, discovery/registration, and fake assertions. Search all static calls and route/listener/command registrations before removing anything. Keep HTTP paths, command signatures, and event contracts stable.
  2. Retain the class or move it to the application's existing actions/services directory. Remove magic traits only after explicit callers exist. Resolve it through ordinary constructor/method injection or app(TheAction::class) at an existing boundary, then call its instance method. Do not replace one global facade with another generic action dispatcher.
  3. Define typed input and a domain result, not a request/response object. HTTP uses a normal controller/FormRequest and resource; Artisan uses a command with explicit output/exit codes; a listener/job adapts the same operation. Inject trusted actor/tenant identity separately from validated untrusted fields.
  4. Enforce shared business invariants and authorization in the operation or a policy it consistently calls. FormRequest validation/authorization only protects HTTP. Jobs and commands need an explicit actor/system policy too. Preserve any WithAttributes preprocessing/validation deliberately; moving code from a controller hook into a shared operation can strengthen behavior but is an intentional change to document.
  5. Put related database writes in the existing connection's DB::transaction, with necessary ownership checks inside the lock/transaction boundary. Propagate exceptions/errors so rollback works. Keep transport rendering outside. Use the app's supported after-commit/outbox mechanisms for nontransactional effects; dispatch failure after a commit is not proof the business write failed.
  6. Replace Action::dispatch with a native job implementing the appropriate queue contract. Copy used connection, queue, retry/backoff, timeout, middleware, missing-model and failure behavior explicitly; don't copy unused knobs. Carry stable IDs/serializable values, reload current records and authorization, and preserve idempotency under repeated deliveries. Queue uniqueness does not guarantee exactly-once effects. Keep legacy job decorators/classes available until old queued payloads drain, or use a separately approved payload migration.
  7. Replace package fakes with observable behavior tests and Laravel's native queue/event/mail fakes only where those boundaries are relevant. Tests that merely assert a fake action was called can miss lost authorization and wrong transaction boundaries.

Example call shape only; RenameProject, Actor, input/result objects, and method names below are app-owned and must match the selected use case:

$result = $renameProject->handle($actor, $input);
// Controller: map $result to the existing resource/status.
// Job: apply the existing retry/failure policy without rendering HTTP.
// Command: render a message and return the documented exit status.

Trial checklist

  • Exercise the real HTTP, direct, CLI, listener, and job entry points that exist for the operation. Same valid command means the same domain result, with appropriate transport output.
  • Unauthorized actor, foreign tenant, missing actor, invalid/blank/oversized input, and revoked job-time authorization leave state unchanged. A direct call cannot bypass an HTTP-only rule that is actually a business invariant.
  • Force failure after the first write and verify no partial commit or success notification from a fresh connection.
  • Deliver a job twice; assert the declared idempotency result and failure/retry behavior. Check queue name, timeout, missing-model policy, and existing serialized job compatibility.
  • Capture JSON versus HTML response behavior, command exit codes, conditional skipped results, and listeners whose adapter method differed from handle.
  • Search for remaining package traits/static calls/fakes. Run composer why lorisleiva/laravel-actions; remove the dependency only once every used feature has an explicit replacement and legacy queued payloads are safe.

Run the application's feature/unit tests and actual production-driver transaction/queue checks. Record PHP/Laravel versions, tested adapters, accepted behavior changes, and commands/output with the change.

Sources

  • MIT reference: lorisleiva/laravel-actions 2.13.0, Concerns/AsObject.php, AsJob.php, AsAction.php, ValidateActions.php, Decorators/ControllerDecorator.php, JobDecorator.php, and object/controller/job/command tests. Retain LICENSE.md for copied/adapted source.
  • Laravel PHP framework baseline. Check supported native validation, dependency-injection, queue, and after-commit APIs against the actual application's lockfile.