BaseController
in package
Class to use models and expose their CRUD as a controller.
Abstract base class for RESTful API controllers. Provides standard CRUD operations (index, show, store, update, destroy) that interact with a model and return JSON responses. Child classes should inject their specific model instance.
Table of Contents
Properties
- $model : mixed
Methods
- __construct() : mixed
- Constructor
- destroy() : void
- Deletes a record
- index() : void
- Lists all records
- show() : void
- Shows a single record
- store() : void
- Creates a new record
- update() : void
- Updates an existing record
Properties
$model
protected
mixed
$model
Methods
__construct()
Constructor
public
__construct(object $model) : mixed
Initializes the controller with a model instance.
Parameters
- $model : object
-
Model instance that handles database operations
destroy()
Deletes a record
public
destroy(int $id) : void
Removes the specified record from the database. Returns success message if deletion is successful.
Parameters
- $id : int
-
The primary key value of the record to delete
Return values
void —Sends JSON response confirming deletion or error
index()
Lists all records
public
index() : void
Retrieves and returns all records from the model.
Return values
void —Sends JSON response with all records
show()
Shows a single record
public
show(int $id) : void
Retrieves and returns a specific record by its ID. Returns 404 response if record is not found.
Parameters
- $id : int
-
The primary key value of the record to retrieve
Return values
void —Sends JSON response with the record or error
store()
Creates a new record
public
store() : void
Reads JSON data from request body, validates it, and creates a new record. Returns the newly created record with 201 status code.
Return values
void —Sends JSON response with created record or error
update()
Updates an existing record
public
update(int $id) : void
Reads JSON data from request body and updates the specified record. Returns the updated record.
Parameters
- $id : int
-
The primary key value of the record to update
Return values
void —Sends JSON response with updated record or error