βοΈ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
makeResolves the action from the container.
MyAction::make();
// Equivalent to:
app(MyAction::class);#run
runResolves and executes the action.
#runIf
runIfResolves and executes the action if the condition is met.
#runUnless
runUnlessResolves and executes the action if some condition is not met.
#__invoke
__invokeExecutes 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
asControllerIt is called when used as an invokable controller. Uses the handle method directly when no asController method exists.
#jsonResponse
jsonResponseCalled 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
htmlResponseCalled 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
getControllerMiddlewareAdds controller middleware directly in the action.
#prepareForValidation
prepareForValidationCalled right before authorization and validation is resolved.
#authorize
authorizeDefines authorization logic for the controller.
You may also return gate responses instead of booleans.
#rules
rulesProvides the validation rules for the controller.
#withValidator
withValidatorAdds custom validation logic to the existing validator.
#afterValidator
afterValidatorAdds an after callback to the existing validator. The example below is equivalent to the example provided in the withValidator method.
#getValidator
getValidatorDefineDefines your validator instead of the default one generated using rules, withValidator, etc.
#getValidationData
getValidationDataDefines the data that should be used for validation. Defaults to: $request->all().
#getValidationMessages
getValidationMessagesCustomize the messages of your validation rules.
#getValidationAttributes
getValidationAttributesProvides some human-friendly mapping to your request attributes.
#getValidationRedirect
getValidationRedirectCustomises the redirect URL when validation fails. Defaults to redirecting back to the previous page.
#getValidationErrorBag
getValidationErrorBagCustomises the validator's error bag when validation fails. Defaults to: default.
#getValidationFailure
getValidationFailureOverrides the validation failure altogether. Defaults to: ValidationException.
#getAuthorizationFailure
getAuthorizationFailureOverrides the authorization failure. Defaults to: AuthorizationException.
Last updated