Overview

Packages

  • CodeIgniter
    • Libraries
    • Rest
  • None

Classes

  • REST_Controller
  • Overview
  • Package
  • Class

Class REST_Controller

CodeIgniter Rest Controller A fully RESTful server implementation for CodeIgniter using one library, one config file and one controller.

CI_Controller
Extended by REST_Controller

Direct known subclasses

Example, Key
Abstract
Package: CodeIgniter\Libraries
Category: Libraries
License: MIT
Author: Phil Sturgeon, Chris Kacerguis
Version: 3.0.0
Link: https://github.com/chriskacerguis/codeigniter-restserver
Located at libraries/REST_Controller.php
Methods summary
protected
# early_checks( )

Extend this function to apply additional checking early on in the process

Extend this function to apply additional checking early on in the process

public
# __construct( string $config = 'rest' )

Constructor for the REST API

Constructor for the REST API

Parameters

$config

Configuration filename minus the file extension e.g: my_rest.php is passed as 'my_rest'

public
# __destruct( )

Deconstructor

Deconstructor

Author

Chris Kacerguis
public
# _remap( string $object_called, array $arguments = [] )

Requests are not made to methods directly, the request will be for an "object". This simply maps the object and method to the correct Controller method

Requests are not made to methods directly, the request will be for an "object". This simply maps the object and method to the correct Controller method

Parameters

$object_called
$arguments
The arguments passed to the controller method
public
# response( array|null $data = NULL, integer|null $http_code = NULL, boolean $continue = FALSE )

Takes mixed data and optionally a status code, then creates the response

Takes mixed data and optionally a status code, then creates the response

Parameters

$data
Data to output to the user
$http_code
HTTP status code
$continue

TRUE to flush the response to the client and continue running the script; otherwise, exit

public
# set_response( array|null $data = NULL, integer|null $http_code = NULL )

Takes mixed data and optionally a status code, then creates the response within the buffers of the Output class. The response is sent to the client lately by the framework, after the current controller's method termination. All the hooks after the controller's method termination are executable

Takes mixed data and optionally a status code, then creates the response within the buffers of the Output class. The response is sent to the client lately by the framework, after the current controller's method termination. All the hooks after the controller's method termination are executable

Parameters

$data
Data to output to the user
$http_code
HTTP status code
protected string|null
# _detect_input_format( )

Get the input format e.g. json or xml

Get the input format e.g. json or xml

Returns

string|null
Supported input format; otherwise, NULL
protected string
# _get_default_output_format( )

Gets the default format from the configuration. Fallbacks to 'json' if the corresponding configuration option $config['rest_default_format'] is missing or is empty

Gets the default format from the configuration. Fallbacks to 'json' if the corresponding configuration option $config['rest_default_format'] is missing or is empty

Returns

string
The default supported input format
protected mixed|null|string
# _detect_output_format( )

Detect which format should be used to output the data

Detect which format should be used to output the data

Returns

mixed|null|string
Output format
protected string|null
# _detect_method( )

Get the HTTP request string e.g. get or post

Get the HTTP request string e.g. get or post

Returns

string|null
Supported request method as a lowercase string; otherwise, NULL if not supported
protected boolean
# _detect_api_key( )

See if the user has provided an API key

See if the user has provided an API key

Returns

boolean
protected string|null
# _detect_lang( )

Preferred return language

Preferred return language

Returns

string|null
The language code
protected boolean
# _log_request( boolean $authorized = FALSE )

Add the request to the log table

Add the request to the log table

Parameters

$authorized
TRUE the user is authorized; otherwise, FALSE

Returns

boolean
TRUE the data was inserted; otherwise, FALSE
protected boolean
# _check_limit( string $controller_method )

Check if the requests to a controller method exceed a limit

Check if the requests to a controller method exceed a limit

Parameters

$controller_method
The method being called

Returns

boolean
TRUE the call limit is below the threshold; otherwise, FALSE
protected boolean
# _auth_override_check( )

Check if there is a specific auth type set for the current class/method/HTTP-method being called

Check if there is a specific auth type set for the current class/method/HTTP-method being called

Returns

boolean
protected
# _parse_get( )

Parse the GET request arguments

Parse the GET request arguments

protected
# _parse_post( )

Parse the POST request arguments

Parse the POST request arguments

protected
# _parse_put( )

Parse the PUT request arguments

Parse the PUT request arguments

protected
# _parse_head( )

Parse the HEAD request arguments

Parse the HEAD request arguments

protected
# _parse_options( )

Parse the OPTIONS request arguments

Parse the OPTIONS request arguments

protected
# _parse_patch( )

Parse the PATCH request arguments

Parse the PATCH request arguments

protected
# _parse_delete( )

Parse the DELETE request arguments

Parse the DELETE request arguments

protected
# _parse_query( )

Parse the query parameters

Parse the query parameters

public array|string|null
# get( null $key = NULL, null $xss_clean = NULL )

Retrieve a value from a GET request

Retrieve a value from a GET request

Parameters

$key

Key to retrieve from the GET request If NULL an array of arguments is returned

$xss_clean
Whether to apply XSS filtering

Returns

array|string|null
Value from the GET request; otherwise, NULL
public array|string|null
# options( null $key = NULL, null $xss_clean = NULL )

Retrieve a value from a OPTIONS request

Retrieve a value from a OPTIONS request

Parameters

$key

Key to retrieve from the OPTIONS request. If NULL an array of arguments is returned

$xss_clean
Whether to apply XSS filtering

Returns

array|string|null
Value from the OPTIONS request; otherwise, NULL
public array|string|null
# head( null $key = NULL, null $xss_clean = NULL )

Retrieve a value from a HEAD request

Retrieve a value from a HEAD request

Parameters

$key

Key to retrieve from the HEAD request If NULL an array of arguments is returned

$xss_clean
Whether to apply XSS filtering

Returns

array|string|null
Value from the HEAD request; otherwise, NULL
public array|string|null
# post( null $key = NULL, null $xss_clean = NULL )

Retrieve a value from a POST request

Retrieve a value from a POST request

Parameters

$key

Key to retrieve from the POST request If NULL an array of arguments is returned

$xss_clean
Whether to apply XSS filtering

Returns

array|string|null
Value from the POST request; otherwise, NULL
public array|string|null
# put( null $key = NULL, null $xss_clean = NULL )

Retrieve a value from a PUT request

Retrieve a value from a PUT request

Parameters

$key

Key to retrieve from the PUT request If NULL an array of arguments is returned

$xss_clean
Whether to apply XSS filtering

Returns

array|string|null
Value from the PUT request; otherwise, NULL
public array|string|null
# delete( null $key = NULL, null $xss_clean = NULL )

Retrieve a value from a DELETE request

Retrieve a value from a DELETE request

Parameters

$key

Key to retrieve from the DELETE request If NULL an array of arguments is returned

$xss_clean
Whether to apply XSS filtering

Returns

array|string|null
Value from the DELETE request; otherwise, NULL
public array|string|null
# patch( null $key = NULL, null $xss_clean = NULL )

Retrieve a value from a PATCH request

Retrieve a value from a PATCH request

Parameters

$key

Key to retrieve from the PATCH request If NULL an array of arguments is returned

$xss_clean
Whether to apply XSS filtering

Returns

array|string|null
Value from the PATCH request; otherwise, NULL
public array|string|null
# query( null $key = NULL, null $xss_clean = NULL )

Retrieve a value from the query parameters

Retrieve a value from the query parameters

Parameters

$key

Key to retrieve from the query parameters If NULL an array of arguments is returned

$xss_clean
Whether to apply XSS filtering

Returns

array|string|null
Value from the query parameters; otherwise, NULL
protected string
# _xss_clean( string $value, boolean $xss_clean )

Sanitizes data so that Cross Site Scripting Hacks can be prevented

Sanitizes data so that Cross Site Scripting Hacks can be prevented

Parameters

$value
Input data
$xss_clean
Whether to apply XSS filtering

Returns

string
public array
# validation_errors( )

Retrieve the validation errors

Retrieve the validation errors

Returns

array
protected boolean
# _perform_ldap_auth( string $username = '', string $password = NULL )

Perform LDAP Authentication

Perform LDAP Authentication

Parameters

$username
The username to validate
$password
The password to validate

Returns

boolean
protected boolean
# _perform_library_auth( string $username = '', string $password = NULL )

Perform Library Authentication - Override this function to change the way the library is called

Perform Library Authentication - Override this function to change the way the library is called

Parameters

$username
The username to validate
$password
The password to validate

Returns

boolean
protected boolean
# _check_login( string $username = NULL, boolean|string $password = FALSE )

Check if the user is logged in

Check if the user is logged in

Parameters

$username
The user's name
$password
The user's password

Returns

boolean
protected
# _check_php_session( )

Check to see if the user is logged in with a PHP session key

Check to see if the user is logged in with a PHP session key

protected
# _prepare_basic_auth( )

Prepares for basic authentication

Prepares for basic authentication

protected
# _prepare_digest_auth( )

Prepares for digest authentication

Prepares for digest authentication

protected
# _check_blacklist_auth( )

Checks if the client's ip is in the 'rest_ip_blacklist' config and generates a 401 response

Checks if the client's ip is in the 'rest_ip_blacklist' config and generates a 401 response

protected
# _check_whitelist_auth( )

Check if the client's ip is in the 'rest_ip_whitelist' config and generates a 401 response

Check if the client's ip is in the 'rest_ip_whitelist' config and generates a 401 response

protected
# _force_login( string $nonce = '' )

Force logging in by setting the WWW-Authenticate header

Force logging in by setting the WWW-Authenticate header

Parameters

$nonce

A server-specified data string which should be uniquely generated each time

protected boolean
# _log_access_time( )

Updates the log table with the total access time

Updates the log table with the total access time

Returns

boolean
TRUE log table updated; otherwise, FALSE

Author

Chris Kacerguis
protected boolean
# _log_response_code( $http_code )

Updates the log table with HTTP response code

Updates the log table with HTTP response code

Parameters

$http_code
HTTP status code

Returns

boolean
TRUE log table updated; otherwise, FALSE

Author

Justin Chen
protected boolean
# _check_access( )

Check to see if the API key has access to the controller and methods

Check to see if the API key has access to the controller and methods

Returns

boolean
TRUE the API key has access; otherwise, FALSE
protected
# _check_cors( )

Checks allowed domains, and adds appropriate headers for HTTP access control (CORS)

Checks allowed domains, and adds appropriate headers for HTTP access control (CORS)

Constants summary
integer HTTP_CONTINUE
# 100
integer HTTP_SWITCHING_PROTOCOLS
# 101
integer HTTP_PROCESSING
# 102
integer HTTP_OK

The request has succeeded

The request has succeeded

# 200
integer HTTP_CREATED

The server successfully created a new resource

The server successfully created a new resource

# 201
integer HTTP_ACCEPTED
# 202
integer HTTP_NON_AUTHORITATIVE_INFORMATION
# 203
integer HTTP_NO_CONTENT

The server successfully processed the request, though no content is returned

The server successfully processed the request, though no content is returned

# 204
integer HTTP_RESET_CONTENT
# 205
integer HTTP_PARTIAL_CONTENT
# 206
integer HTTP_MULTI_STATUS
# 207
integer HTTP_ALREADY_REPORTED
# 208
integer HTTP_IM_USED
# 226
integer HTTP_MULTIPLE_CHOICES
# 300
integer HTTP_MOVED_PERMANENTLY
# 301
integer HTTP_FOUND
# 302
integer HTTP_SEE_OTHER
# 303
integer HTTP_NOT_MODIFIED

The resource has not been modified since the last request

The resource has not been modified since the last request

# 304
integer HTTP_USE_PROXY
# 305
integer HTTP_RESERVED
# 306
integer HTTP_TEMPORARY_REDIRECT
# 307
integer HTTP_PERMANENTLY_REDIRECT
# 308
integer HTTP_BAD_REQUEST

The request cannot be fulfilled due to multiple errors

The request cannot be fulfilled due to multiple errors

# 400
integer HTTP_UNAUTHORIZED

The user is unauthorized to access the requested resource

The user is unauthorized to access the requested resource

# 401
integer HTTP_PAYMENT_REQUIRED
# 402
integer HTTP_FORBIDDEN

The requested resource is unavailable at this present time

The requested resource is unavailable at this present time

# 403
integer HTTP_NOT_FOUND

The requested resource could not be found

The requested resource could not be found

Note: This is sometimes used to mask if there was an UNAUTHORIZED (401) or FORBIDDEN (403) error, for security reasons

# 404
integer HTTP_METHOD_NOT_ALLOWED

The request method is not supported by the following resource

The request method is not supported by the following resource

# 405
integer HTTP_NOT_ACCEPTABLE

The request was not acceptable

The request was not acceptable

# 406
integer HTTP_PROXY_AUTHENTICATION_REQUIRED
# 407
integer HTTP_REQUEST_TIMEOUT
# 408
integer HTTP_CONFLICT

The request could not be completed due to a conflict with the current state of the resource

The request could not be completed due to a conflict with the current state of the resource

# 409
integer HTTP_GONE
# 410
integer HTTP_LENGTH_REQUIRED
# 411
integer HTTP_PRECONDITION_FAILED
# 412
integer HTTP_REQUEST_ENTITY_TOO_LARGE
# 413
integer HTTP_REQUEST_URI_TOO_LONG
# 414
integer HTTP_UNSUPPORTED_MEDIA_TYPE
# 415
integer HTTP_REQUESTED_RANGE_NOT_SATISFIABLE
# 416
integer HTTP_EXPECTATION_FAILED
# 417
integer HTTP_I_AM_A_TEAPOT
# 418
integer HTTP_UNPROCESSABLE_ENTITY
# 422
integer HTTP_LOCKED
# 423
integer HTTP_FAILED_DEPENDENCY
# 424
integer HTTP_RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL
# 425
integer HTTP_UPGRADE_REQUIRED
# 426
integer HTTP_PRECONDITION_REQUIRED
# 428
integer HTTP_TOO_MANY_REQUESTS
# 429
integer HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE
# 431
integer HTTP_INTERNAL_SERVER_ERROR

The server encountered an unexpected error

The server encountered an unexpected error

Note: This is a generic error message when no specific message is suitable

# 500
integer HTTP_NOT_IMPLEMENTED

The server does not recognise the request method

The server does not recognise the request method

# 501
integer HTTP_BAD_GATEWAY
# 502
integer HTTP_SERVICE_UNAVAILABLE
# 503
integer HTTP_GATEWAY_TIMEOUT
# 504
integer HTTP_VERSION_NOT_SUPPORTED
# 505
integer HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL
# 506
integer HTTP_INSUFFICIENT_STORAGE
# 507
integer HTTP_LOOP_DETECTED
# 508
integer HTTP_NOT_EXTENDED
# 510
integer HTTP_NETWORK_AUTHENTICATION_REQUIRED
# 511
Properties summary
protected string|null $rest_format

This defines the rest format Must be overridden it in a controller so that it is set

This defines the rest format Must be overridden it in a controller so that it is set

# NULL
protected array $methods

Defines the list of method properties such as limit, log and level

Defines the list of method properties such as limit, log and level

# []
protected array $allowed_http_methods

List of allowed HTTP methods

List of allowed HTTP methods

# ['get', 'delete', 'post', 'put', 'options', 'patch', 'head']
protected object $request

Contains details about the request Fields: body, format, method, ssl Note: This is a dynamic object (stdClass)

Contains details about the request Fields: body, format, method, ssl Note: This is a dynamic object (stdClass)

# NULL
protected object $response

Contains details about the response Fields: format, lang Note: This is a dynamic object (stdClass)

Contains details about the response Fields: format, lang Note: This is a dynamic object (stdClass)

# NULL
protected object $rest

Contains details about the REST API Fields: db, ignore_limits, key, level, user_id Note: This is a dynamic object (stdClass)

Contains details about the REST API Fields: db, ignore_limits, key, level, user_id Note: This is a dynamic object (stdClass)

# NULL
protected array $_get_args

The arguments for the GET request method

The arguments for the GET request method

# []
protected array $_post_args

The arguments for the POST request method

The arguments for the POST request method

# []
protected array $_put_args

The arguments for the PUT request method

The arguments for the PUT request method

# []
protected array $_delete_args

The arguments for the DELETE request method

The arguments for the DELETE request method

# []
protected array $_patch_args

The arguments for the PATCH request method

The arguments for the PATCH request method

# []
protected array $_head_args

The arguments for the HEAD request method

The arguments for the HEAD request method

# []
protected array $_options_args

The arguments for the OPTIONS request method

The arguments for the OPTIONS request method

# []
protected array $_query_args

The arguments for the query parameters

The arguments for the query parameters

# []
protected array $_args

The arguments from GET, POST, PUT, DELETE, PATCH, HEAD and OPTIONS request methods combined

The arguments from GET, POST, PUT, DELETE, PATCH, HEAD and OPTIONS request methods combined

# []
protected string $_insert_id

The insert_id of the log entry (if we have one)

The insert_id of the log entry (if we have one)

# ''
protected boolean $_allow

If the request is allowed based on the API key provided

If the request is allowed based on the API key provided

# TRUE
protected string $_user_ldap_dn

The LDAP Distinguished Name of the User post authentication

The LDAP Distinguished Name of the User post authentication

# ''
protected string $_start_rtime

The start of the response time from the server

The start of the response time from the server

# ''
protected string $_end_rtime

The end of the response time from the server

The end of the response time from the server

# ''
protected array $_supported_formats

List all supported methods, the first will be the default format

List all supported methods, the first will be the default format

# [ 'json' => 'application/json', 'array' => 'application/json', 'csv' => 'application/csv', 'html' => 'text/html', 'jsonp' => 'application/javascript', 'php' => 'text/plain', 'serialized' => 'application/vnd.php.serialized', 'xml' => 'application/xml' ]
protected object $_apiuser

Information about the current API user

Information about the current API user

#
protected boolean $check_cors

Whether or not to perform a CORS check and apply CORS headers to the request

Whether or not to perform a CORS check and apply CORS headers to the request

# NULL
protected boolean $_enable_xss

Enable XSS flag Determines whether the XSS filter is always active when GET, OPTIONS, HEAD, POST, PUT, DELETE and PATCH data is encountered Set automatically based on config setting

Enable XSS flag Determines whether the XSS filter is always active when GET, OPTIONS, HEAD, POST, PUT, DELETE and PATCH data is encountered Set automatically based on config setting

# FALSE
protected array $http_status_codes

HTTP status codes and their respective description Note: Only the widely used HTTP status codes are used

HTTP status codes and their respective description Note: Only the widely used HTTP status codes are used

Link

http://www.restapitutorial.com/httpstatuscodes.html
# [ self::HTTP_OK => 'OK', self::HTTP_CREATED => 'CREATED', self::HTTP_NO_CONTENT => 'NO CONTENT', self::HTTP_NOT_MODIFIED => 'NOT MODIFIED', self::HTTP_BAD_REQUEST => 'BAD REQUEST', self::HTTP_UNAUTHORIZED => 'UNAUTHORIZED', self::HTTP_FORBIDDEN => 'FORBIDDEN', self::HTTP_NOT_FOUND => 'NOT FOUND', self::HTTP_METHOD_NOT_ALLOWED => 'METHOD NOT ALLOWED', self::HTTP_NOT_ACCEPTABLE => 'NOT ACCEPTABLE', self::HTTP_CONFLICT => 'CONFLICT', self::HTTP_INTERNAL_SERVER_ERROR => 'INTERNAL SERVER ERROR', self::HTTP_NOT_IMPLEMENTED => 'NOT IMPLEMENTED' ]
API documentation generated by ApiGen