- Definition & Principles
- Folder Structure
- Code Sample
- Controller response builder helper functions
Read from the Porto SAP Documentation (#Controllers).
- All API Controller MUST extend from
- All Web Controller MUST extend from
- Controllers should use the function
callto call Actions. (do not manually inject the Action and invoke the
- Controllers should pass the Request object to the Action instead of passing data from the request. The Request object is the best class to store the state of the Request during its life cycle.
User Web Welcome Controller:
User API Login Controller:
Notice we call the Action using
Apiato::call() which triggers the
run function in the Action as well inform the action which UI called it, (
$this->getUI()) in case you want to handle the same Action differently based on the UI type.
The second parameter of the
call function is an array of the Action parameters in order. When you need to pass data to the Action, it's recommended to pass the Request Object as it should be the place that holds the state of your current request.
Refer to the Magical Call page for more info and examples on how to use the call function.
Example: Usage from Routes Endpoint:
Many helper function are there to help you build your response faster, those helpers exist in the
transform This is the most useful function which you will be using in most cases.
- First required parameter accepts data as object or Collection of objects.
- Second required parameter is the transformer object
- Third optional parameter take the includes that should be returned by the response, ($availableIncludes and $defaultIncludes in the transformer class).
- Fourth optional parameter accepts metadata to be injected in the response.
withMeta This function allows including metadata in the response.
json This function allows passing array data to be represented as json.
- // Some functions might not be documented, so refer to the
vendor/apiato/core/Traits/ResponseTrait.phpand see the public functions.