WiseLoop PHP REST Services 3.3.1
RESTful web service API framework for PHP
wlRestController Class Reference
Inheritance diagram for wlRestController:

Public Member Functions

 __construct ($name=null)
 setService ($restService)
 getService ()
 getName ()
 call ($actionName)
 getDefaultName ()
 getVersion ()
 initialize ()
 getHelp ()
 handleException ($ex)

Data Fields

const DEFAULT_ACTION_NAME = 'defaultAction'

Protected Member Functions

 defaultAction ()

Protected Attributes

 $queryParams
 $actionParams

Private Member Functions

 getControllerDescription ()

Private Attributes

 $_restService = null
 $_name

Detailed Description

WiseLoop RestController class definition
This generic class provides a global way to create service actions.
A service controller consists of actual actions responding to requests.
To create a service controller, the developer must extend wlRestController class and define some actions that will process the requests and provide data to be served back to the requester. The actions are implemented by class methods (declared as protected or public) following the convention: actionNameRequestMethod. Request method must be Get, Post, Put or Delete.
Ex.: Method addGet implements action add that accepts only GET requests.

 class sampleHelloRestControllerV1 extends wlRestController {
    protected function helloGet() {
        $params = $this->getService()->getRequest()->getQueryParams();
        $name = $params[0];
        return "Hello $name (from GET)";
    }
 }

The service controller instance must then be registered to a wlRestService object.

$service = new wlRestService();
$service->registerController(new sampleHelloRestControllerV1('hello'));
$service->run();
Author:
WiseLoop

Constructor & Destructor Documentation

__construct ( name = null)

Constructor.
Creates a wlRestController object.

Parameters:
string$namethe service controller name
The name is important because it is used to identify the actual controller instance by finding a match between the name provided in the request URL and the wlRestService registered controllers. If no name is provided in the constructor, the real derived controller class must re-implement the wlRestController::getDefaultName method.
See also:
wlRestRequest, wlRestService::registerController

Member Function Documentation

call ( actionName)

Calls an action (method) of the service controller.
The given action name is provided by the parsed request URL against the URL route format specified in settings file.
The actual method that will be called is computed as a concatenation between the given $actionName and the request method (wlRestRequest::getMethod). This is the mechanism for linking an action to a request method type: the action name must be followed by the allowed request methods: Get, Post, Put, Delete.
Ex.: if in the route format (given in the default rest-settings.ini settings file or another .ini file) is route=controller/version/action and the request URL (submitted by GET request method) is http ://rest-service-url-endpoint/math/v1/add/1/2/3/4, the service controller named math, version v1 must have a method addGet. The addGet method has to be declared as protected or public. If method addGet does exists, an exception will be thrown.

Parameters:
string$actionNamethe action name (taken previously from the URL)
Exceptions:
wlRestException
Returns:
mixed|string
See also:
wlRestRequest, wlRestService::setRoute
defaultAction ( ) [protected]

Calls a default action if not explicit action name is provided in the URL.
This method returns by default what wlRestController::getHelp returns, but it can be reimplemented in the actual derived controller classes.

Returns:
string|null
See also:
wlRestController::getHelp
getControllerDescription ( ) [private]

This method is used internally by wlRestController::getHelp to get a list with action names and the corresponding acceptable requests methods.

Returns:
array
getDefaultName ( )

Returns the default controller name if no name is provided at construction time.
This method returns null by default, but it must be reimplemented in the actual derived controller if no name is provided in the constructor.

Returns:
string|null
getHelp ( )

Returns a formatted list with action names and the corresponding acceptable requests methods.
Results of this method will be served by default if no explicit action is provided in the request URL.

Returns:
string
getName ( )

Returns the name of the current service controller.
The name is used to identify the actual controller instance by finding a match between the name provided in the request URL and the registered controllers.

Returns:
string
getService ( )

Returns the REST service that has registered the current controller

Returns:
wlRestService
getVersion ( )

Returns the default version of the current service controller.
This method returns v1 by default, but it can be reimplemented in the actual derived controller classes.

Returns:
string
handleException ( ex)

Handles any exception during an action call.
By default this method just rethrows the exception, but it can be reimplemented in the actual derived controller classes to take more custom actions upon the exception. Anyway, regardless of the custom actions taken, finally the exception should be re-thrown in order to be catch by the global wlRestService exception handler and formatted and served properly to the requester.

Parameters:
wlRestException$ex
Exceptions:
wlRestException
initialize ( )

Performs some general initializations before calling the action.
This method does nothing by default, but it can be reimplemented in the actual derived controller classes.

Returns:
void
setService ( restService)

Sets the REST service that has registered the current controller

Parameters:
wlRestService$restService
Returns:
void

Field Documentation

$_name [private]

Type: string service controller name given at construction time

$_restService = null [private]

Type: wlRestService the REST services that has registered the current controller

$actionParams [protected]

Type: mixed action parameters as returned by the current request wlRestRequest::getActionParams method

$queryParams [protected]

Type: array query GET parameters as returned by the current request wlRestRequest::getQueryParams method

const DEFAULT_ACTION_NAME = 'defaultAction'
 All Data Structures Functions Variables