class Controller extends Controller

The main Controler to inherit from.

Properties

protected array $key_mapping Maps request parameters to database columns.
protected array $relations The relations to load with the model from the DB.
protected Formatter $formatter_class Use this to simply override the Formatter.
protected ModelMapping $model_mapping_class Use this to simply override the ModelMapping.
protected RequestAdapter $request_adapter_class Use this to simply override the RequestAdapter.
$request_adapter
$request
protected $model
protected $model_mapping
protected $formatter
protected $input
protected $resource
protected $collection

Methods

__construct( Request $request)

No description

Response
index()

Request Handler: List all resources.

Response
store()

Request Handler: Create a new resource.

Response
show()

Request Handler: Show a single resource.

Response
update()

Request Handler: Update a resource.

Response
destroy()

Request Handler: Delete a resource.

void
getResource()

Fetch a single record from the DB and store it to $this->resource.

void
getCollection()

Fetch a Collection of Resources from the databse and store it to $this->collection.

void
formatResource()

Generate the response data using our Formatter.

void
formatCollection()

Generate the response data using our Formatter.

Builder
filterByRequest( Builder $query, Array $mapping = null)

Apply filters based on the $key_mapping. If a key is present in the request, an appropriate where clause will be added to the query.

void
gatherInput()

Get the request data from the adapter and store it to $this->input.

void
validateInput( bool $only_present = false)

Validate the input data using the appropriate validator.

void
createResource()

Create a new instance of the current Model, fill it with the input data, save it to the database and load it anew to get all attributes populated.

void
updateResource()

Update the resource with the input data.

void
destroyResource()

Delete the resource.

makeResponse($response_data = null, $status_code = 200)

No description

void
refreshResource()

Load a fresh instance of the current resource from the database.

makeModelMapping()

Return a ModelMapping instance.

makeFormatter( ModelMapping $mapping)

Return a Formatter instance.

makeRequestAdapter( Request $request)

Return a RequestAdapter instance.

void
authorizeAction( String $action)

Make sure the authenticated user is allowed to perform this type of $action.

void
authorizeResource( String $action)

Make sure the authenticated user is allowed to perform this type of $action on $this->resource.

Builder
adaptResourceQuery( Builder $query)

A hook to customize the query for all single resource queries.

Builder
adaptCollectionQuery( Builder $query)

A hook to customize the query for all collection queries.

Array
adaptRules( array $rules)

This is the place to manipulate the validation rules at runtime.

void
beforeSave()

Hook in here to customize the input before saving a resource.

void
afterSave()

Hook in here to customize actions after saving a resource

void
beforeCreate()

Hook in here to customize the input before creating a resource.

void
beforeUpdate()

Hook in here to customize the input before updating a resource.

void
afterConstruct()

Use this hook to apply custom logic after the controller instance has been created.

Details

at line line 102
__construct( Request $request)

Parameters

Request $request

at line line 123
Response index()

Request Handler: List all resources.

Return Value

Response

at line line 136
Response store()

Request Handler: Create a new resource.

Return Value

Response

at line line 151
Response show()

Request Handler: Show a single resource.

Return Value

Response

at line line 165
Response update()

Request Handler: Update a resource.

Return Value

Response

at line line 182
Response destroy()

Request Handler: Delete a resource.

Return Value

Response

at line line 204
protected void getResource()

Fetch a single record from the DB and store it to $this->resource.

Return Value

void

Exceptions

NotFound In case no record matches the query

at line line 222
protected void getCollection()

Fetch a Collection of Resources from the databse and store it to $this->collection.

Return Value

void

at line line 235
protected void formatResource()

Generate the response data using our Formatter.

Return Value

void

at line line 245
protected void formatCollection()

Generate the response data using our Formatter.

Return Value

void

at line line 258
protected Builder filterByRequest( Builder $query, Array $mapping = null)

Apply filters based on the $key_mapping. If a key is present in the request, an appropriate where clause will be added to the query.

Parameters

Builder $query The query to apply the filters to
Array $mapping (optional) A mapping to use instead of $this->key_mapping

Return Value

Builder

at line line 291
protected void gatherInput()

Get the request data from the adapter and store it to $this->input.

Return Value

void

at line line 302
protected void validateInput( bool $only_present = false)

Validate the input data using the appropriate validator.

Parameters

bool $only_present Whether to only validate fields present in $this->input

Return Value

void

at line line 316
protected void createResource()

Create a new instance of the current Model, fill it with the input data, save it to the database and load it anew to get all attributes populated.

Return Value

void

at line line 341
protected void updateResource()

Update the resource with the input data.

Return Value

void

at line line 356
protected void destroyResource()

Delete the resource.

Return Value

void

at line line 361
protected makeResponse($response_data = null, $status_code = 200)

Parameters

$response_data
$status_code

at line line 375
protected void refreshResource()

Load a fresh instance of the current resource from the database.

Return Value

void

at line line 395
protected ModelMapping makeModelMapping()

Return a ModelMapping instance.

This can be used to dynamically customize the mapping, for example based on Auth/Roles.

Return Value

ModelMapping

at line line 408
protected Formatter makeFormatter( ModelMapping $mapping)

Return a Formatter instance.

This can be used to dynamically customize the formatter.

Parameters

ModelMapping $mapping The ModelMapping instance to use

Return Value

Formatter

at line line 421
protected RequestAdapter makeRequestAdapter( Request $request)

Return a RequestAdapter instance.

This can be used to dynamically customize the adapter.

Parameters

Request $request The current Request object

Return Value

RequestAdapter

at line line 433
protected void authorizeAction( String $action)

Make sure the authenticated user is allowed to perform this type of $action.

Parameters

String $action The name of the action

Return Value

void

at line line 442
protected void authorizeResource( String $action)

Make sure the authenticated user is allowed to perform this type of $action on $this->resource.

Parameters

String $action The name of the action

Return Value

void

at line line 450
protected Builder adaptResourceQuery( Builder $query)

A hook to customize the query for all single resource queries.

Parameters

Builder $query The original query

Return Value

Builder The customized query

at line line 461
protected Builder adaptCollectionQuery( Builder $query)

A hook to customize the query for all collection queries.

Parameters

Builder $query The original query

Return Value

Builder The customized query

at line line 472
protected Array adaptRules( array $rules)

This is the place to manipulate the validation rules at runtime.

Parameters

array $rules The original rules from the Validator

Return Value

Array The adapted rules

at line line 484
protected void beforeSave()

Hook in here to customize the input before saving a resource.

This happens in both the create and update actions.

Return Value

void

at line line 493
protected void afterSave()

Hook in here to customize actions after saving a resource

This happens in both the create and update actions.

Return Value

void

at line line 500
protected void beforeCreate()

Hook in here to customize the input before creating a resource.

Return Value

void

at line line 507
protected void beforeUpdate()

Hook in here to customize the input before updating a resource.

Return Value

void

at line line 515
protected void afterConstruct()

Use this hook to apply custom logic after the controller instance has been created.

Return Value

void