ApiBaseController
extends Controller
in package
Base controller for REST API v1 endpoints.
Provides standardized JSON response methods, pagination helpers, request body parsing, and input validation for all API controllers.
Tags
Table of Contents
Properties
- $layout : string
Methods
- baseUrl() : string
- Get the base URL for the application.
- buildPagination() : array<string|int, mixed>
- Build pagination metadata for views.
- businessId() : int
- Get the authenticated business ID from API context.
- clearOldInput() : void
- Clear stored old input data.
- error() : void
- Return an error JSON response.
- flash() : void
- Set a flash message in the session.
- flashOldInput() : void
- Store current POST data as old input for form repopulation.
- getJsonBody() : array<string|int, mixed>
- Parse JSON request body.
- getPage() : int
- Get the current page number from the query string.
- getPaginationParams() : array{page: int, limit: int, offset: int}
- Get pagination parameters from query string.
- json() : void
- Send a JSON response.
- old() : mixed
- Get previous form input after validation failure.
- paginationMeta() : array<string|int, mixed>
- Build pagination metadata for a response.
- queryParam() : mixed
- Get a query string parameter with optional default.
- redirect() : void
- Send an HTTP redirect.
- requirePermission() : void
- Require that the authenticated client user has a specific permission.
- setCorsHeaders() : void
- Set CORS headers for the response.
- success() : void
- Return a successful JSON response.
- url() : string
- Generate a full URL for a given path.
- validateRequired() : array<string|int, string>
- Validate required fields in a data array.
- view() : void
- Render a view template wrapped in a layout.
Properties
$layout
protected
string
$layout
= 'main'
Default layout template
Methods
baseUrl()
Get the base URL for the application.
protected
baseUrl() : string
Return values
stringbuildPagination()
Build pagination metadata for views.
protected
buildPagination(int $total, int $page, int $perPage, string $baseUrl) : array<string|int, mixed>
Parameters
- $total : int
-
Total record count
- $page : int
-
Current page number
- $perPage : int
-
Records per page
- $baseUrl : string
-
Base URL path (e.g., "client/orders")
Return values
array<string|int, mixed> —Pagination metadata
businessId()
Get the authenticated business ID from API context.
protected
businessId() : int
Return values
intclearOldInput()
Clear stored old input data.
protected
clearOldInput() : void
error()
Return an error JSON response.
protected
error(string $error, string $message[, int $status = 400 ][, array<string|int, mixed>|null $details = null ]) : void
Parameters
- $error : string
-
Error code
- $message : string
-
Human-readable message
- $status : int = 400
-
HTTP status code
- $details : array<string|int, mixed>|null = null
-
Validation errors or extra info
flash()
Set a flash message in the session.
protected
flash(string $key, string $message) : void
Parameters
- $key : string
-
Flash message key (e.g., "success", "error")
- $message : string
-
The message text
flashOldInput()
Store current POST data as old input for form repopulation.
protected
flashOldInput() : void
getJsonBody()
Parse JSON request body.
protected
getJsonBody() : array<string|int, mixed>
Return values
array<string|int, mixed> —Parsed body or empty array
getPage()
Get the current page number from the query string.
protected
getPage() : int
Return values
int —Page number (minimum 1)
getPaginationParams()
Get pagination parameters from query string.
protected
getPaginationParams([int $defaultLimit = 25 ][, int $maxLimit = 100 ]) : array{page: int, limit: int, offset: int}
Parameters
- $defaultLimit : int = 25
-
Default items per page
- $maxLimit : int = 100
-
Maximum allowed limit
Return values
array{page: int, limit: int, offset: int}json()
Send a JSON response.
protected
json(mixed $data[, int $status = 200 ]) : void
Parameters
- $data : mixed
-
Data to encode as JSON
- $status : int = 200
-
HTTP status code
old()
Get previous form input after validation failure.
protected
old(string $field[, mixed $default = '' ]) : mixed
Parameters
- $field : string
-
The form field name
- $default : mixed = ''
-
Default value if not found
paginationMeta()
Build pagination metadata for a response.
protected
paginationMeta(int $total, int $page, int $limit) : array<string|int, mixed>
Parameters
- $total : int
-
Total record count
- $page : int
-
Current page
- $limit : int
-
Items per page
Return values
array<string|int, mixed>queryParam()
Get a query string parameter with optional default.
protected
queryParam(string $key[, mixed $default = null ]) : mixed
Parameters
- $key : string
-
Parameter name
- $default : mixed = null
-
Default value
redirect()
Send an HTTP redirect.
protected
redirect(string $url) : void
Parameters
- $url : string
-
The URL to redirect to
requirePermission()
Require that the authenticated client user has a specific permission.
protected
requirePermission(string $permission) : void
Redirects to the client dashboard with an error flash if denied.
Parameters
- $permission : string
-
Permission key (e.g., 'menu_manage')
setCorsHeaders()
Set CORS headers for the response.
protected
setCorsHeaders() : void
success()
Return a successful JSON response.
protected
success(mixed $data[, int $status = 200 ][, array<string|int, mixed> $meta = [] ]) : void
Parameters
- $data : mixed
-
Response data
- $status : int = 200
-
HTTP status code
- $meta : array<string|int, mixed> = []
-
Optional metadata (pagination, etc.)
url()
Generate a full URL for a given path.
protected
url([string $path = '' ]) : string
Parameters
- $path : string = ''
-
The path to append to the base URL
Return values
stringvalidateRequired()
Validate required fields in a data array.
protected
validateRequired(array<string|int, mixed> $data, array<string|int, string> $fields) : array<string|int, string>
Parameters
- $data : array<string|int, mixed>
-
Input data
- $fields : array<string|int, string>
-
Required field names
Return values
array<string|int, string> —Array of missing field names (empty if valid)
view()
Render a view template wrapped in a layout.
protected
view(string $template[, array<string|int, mixed> $data = [] ]) : void
Parameters
- $template : string
-
Path to view relative to app/Views/ (e.g., "admin/dashboard")
- $data : array<string|int, mixed> = []
-
Variables to extract into the view scope