Api Controller Manager
The ApiController class is an abstract class that manages some features to help you complete your requests rapidly.
A Basic Api Controller Example#
The following snippet shows the basic usage of the ApiController class.
Let's see what is in this snippet.
- REPOSITORY_NAME: we set the repository shorthand name which is defined in the Mongez Config File in the
repositoriessection - $this->repsoitory: the instance of users repository, will be autmatically available once
REPOSITORY_NAMEis defined. - success method: returns a success
200response with the passed array data, if no arguments is passed to that method, ['success' => true] will be returned instead. - notFound method: returns a failure
404response with the passed string message, if no arguments is passed to that method, trans('response.notFound') will be returned instead.Check the response error schema for returned errors.
Using other repositories#
The ApiController class uses RepositoryTrait which means you can call any repository in any method.
Available Methods#
success Method#
public success(array $data = null): Response
Returns 200 response code with the provided data.
If no passed arguments, then ['success' => true] will be returned by default.
Default returned value can be overriden from Mongez Config File under
response.defaults.successkey.
This method triggers response.success event and passes the passed data argument to it.
successCreate Method#
public successCreate(array $data = null): Response
Returns 201 response code with the provided data.
This method should be used with creating new records.
If no passed arguments, then ['success' => true] will be returned by default.
Default returned value can be overriden from Mongez Config File under
response.defaults.successCreatekey.
This method triggers response.successCreate event and passes the passed data argument to it.
notFound Method#
public notFound(string $data = null): Response
Returns 404 response code with the provided data.
If no passed arguments, then trans('response.notFound') will be returned by default.
Default returned value can be overriden from Mongez Config File under
mongez.response.defaults.notFoundkey.
This method triggers response.notFound event and passes the passed data argument to it.
See Response Error Schema for the returned response schema.
badRequest Method#
public badRequest(string|MessageBag $data): Response
Returns 400 response code with the provided data.
Laravel MessageBag or string can be passed.
Default returned value can be overriden from Mongez Config File under
response.success.defaultkey.
This method triggers response.badRequest event and passes the passed data argument to it.
See Response Error Schema for the returned response schema.
unauthorized Method#
public notFound(mixed $data = null): Response
Returns 422 response code with the provided data.
If no passed arguments, then trans('response.unauthorized') will be returned by default.
Default returned value can be overriden from Mongez Config File under
response.defaults.unauthorizedkey.
This method triggers response.unauthorized event and passes the passed data argument to it.
See Response Error Schema for the returned response schema.
send Method#
public send(array $data, int $statusCode): Response
Returns the given data and the response code.
This method triggers response.send event and passes the passed data and response status code as arguments for the event callbacks.
Manipulating Returned Values#
Sometimes you want to send extra data in all requests, or in success requests for example, you can achieve this by listening to any of the response events and return data
Please note that if any of the previous method events returned array value then it will be returned instead.
So for instance if we want to return the me object that contains current user data for all responses, then we can as follows:
- Go to mongez config file.
- Under
eventskey, addresponse.sendkey with an array. - In the array add another array that holds the event class as first element and method name as second element.
Now we navigate to our class App\Modules\Users\Events\ReturnMeObject which should be located in app/Modules/Users/Events/ReturnMeObject.php
Response Error Schema#
The returned error can be customized how it should be returned.
The response schema can be overriden in Mongez Config under
response.errorkey.
The response can be one of the following:
- String
- Laravel MessageBag
- Associated Array
- Indexed Array
If the response is a string then it will be converted into an associated array as 'error' => '$errorMessageString.
If the response is a Laravel MessageBag then it will be converted into an associated array as 'inputName' => 'firstError and will take only first error for each input.
You can override the returned messages from Laravel MessageBag in the monez config file under
mongez.response.error.maxArrayLength.
Resposne Error Defaults#
The following snippet illustrates the default values that will be used for returend errors.
Resposne Error Schema Strategies#
There are two strategies: array and object.
The array strategy will convert the errors into an array of object, each object contains a key which holds the input name and value which holds the error message.
The response shape will be as follows:
Please note that only string and Laravel MessageBag will be converted, if the passed data is an array it will be returned the same.
The object strategy will convert the errors into object with a key which holds the input name and value which holds the error message.
The response shape will be as follows:
Please note that only string and Laravel MessageBag will be converted, if the passed data is an array it will be returned the same.