βš™οΈActions

Classes that take care of one specific task.

This package introduces a new way of organizing the logic of your Maginium applications by focusing on the actions your applications provide.

Instead of creating controllers, jobs, listeners, and so on, it allows you to create a PHP class that handles a specific task and run that class as anything you want.

#make

Resolves the action from the container.

MyAction::make();

// Equivalent to:
app(MyAction::class);

#run

Resolves and executes the action.

#runIf

Resolves and executes the action if the condition is met.

#runUnless

Resolves and executes the action if some condition is not met.

#__invoke

Executes the action by delegating immediately to the handle method.

Whilst this method is not used, it has to be defined on the action to register the action as an invokable controller. When missing, Maginium will throw an exception warning us that we're trying to register a class as an invokable controller without the __invoke method. The truth is, the controller will be an instance of ControllerDecorator but the framework doesn't know that yet.

If you need to use the __invoke method for something else, you may override it (opens a new window) with anything you want. The only requirement is that a __invoke method has to exist.

#Methods used

Lists all methods recognized and used by the ControllerDecorator

#asController

It is called when used as an invokable controller. Uses the handle method directly when no asController method exists.

#jsonResponse

Called after the asController method when the request expects JSON. The first argument is the return value of the asController method and the second argument is the request itself.

#htmlResponse

Called after the asController method when the request expects HTML. The first argument is the return value of the asController method and the second argument is the request itself.

#getControllerMiddleware

Adds controller middleware directly in the action.

#prepareForValidation

Called right before authorization and validation is resolved.

#authorize

Defines authorization logic for the controller.

You may also return gate responses instead of booleans.

#rules

Provides the validation rules for the controller.

#withValidator

Adds custom validation logic to the existing validator.

#afterValidator

Adds an after callback to the existing validator. The example below is equivalent to the example provided in the withValidator method.

#getValidator

DefineDefines your validator instead of the default one generated using rules, withValidator, etc.

#getValidationData

Defines the data that should be used for validation. Defaults to: $request->all().

#getValidationMessages

Customize the messages of your validation rules.

#getValidationAttributes

Provides some human-friendly mapping to your request attributes.

#getValidationRedirect

Customises the redirect URL when validation fails. Defaults to redirecting back to the previous page.

#getValidationErrorBag

Customises the validator's error bag when validation fails. Defaults to: default.

#getValidationFailure

Overrides the validation failure altogether. Defaults to: ValidationException.

#getAuthorizationFailure

Overrides the authorization failure. Defaults to: AuthorizationException.

Last updated