\PhalApi_Request

Request Class

  • reponsible for building params by rules and returning error message
  • need to be use with API rules together

Summary

Methods
Properties
Constants
__construct()
getHeader()
get()
getByRule()
getAll()
getService()
getServiceApi()
getServiceAction()
No public properties found
No constants found
genData()
getAllHeaders()
getDataBySource()
$data
$get
$post
$request
$cookie
$headers
$apiName
$actionName
N/A
No private methods found
No private properties found
N/A

Properties

$data

$data : array

Type

array — 主数据源,接口原始参数

$get

$get : 

@var array $get 备用数据源 $_GET

Type

$post

$post : 

@var array $post 备用数据源 $_POST

Type

$request

$request : 

@var array $request 备用数据源 $_REQUEST

Type

$cookie

$cookie : 

@var array $cookie 备用数据源 $_COOKIE

Type

$headers

$headers : array

Type

array — 备用数据源 请求头部信息

$apiName

$apiName : 

Type

$actionName

$actionName : string

Type

string — 接口服务方法名

Methods

__construct()

__construct(array  $data = NULL) 

- 如果需要定制已知的数据源(即已有数据成员),则可重载此方法,例

class My_Request extend PhalApi_Request{
    public function __construct($data = NULL) {
        parent::__construct($data);

        // handle json
        $this->post = json_decode(file_get_contents('php://input'), TRUE);

        // handle xml
        $this->post = simplexml_load_string (
            file_get_contents('php://input'),
            'SimpleXMLElement',
            LIBXML_NOCDATA
        );
        $this->post = json_decode(json_encode($this->post), TRUE);
    }
}
  • 其他格式或其他xml可以自行写函数处理

Parameters

array $data

参数来源,可以为:$_GET/$_POST/$_REQUEST/自定义

getHeader()

getHeader(string  $key, mixed  $default = NULL) : string

Get specified header parameter

Parameters

string $key

header key

mixed $default

default value

Returns

string

get()

get(string  $key, mixed  $default = NULL) : mixed

Get one API parameter by name

Parameters

string $key

parameter name

mixed $default

default value

Returns

mixed

getByRule()

getByRule(array  $rule) : mixed

Get parameter by rule

build the pramater with rule, and reutrn error message when fail

Parameters

array $rule

rule, such as: array('name' => '', 'type' => '', 'defalt' => ...)

Throws

\PhalApi_Exception_BadRequest
\PhalApi_Exception_InternalServerError

Returns

mixed

getAll()

getAll() : array

Get all the params

Returns

array

getService()

getService() : string

Get service name

  • override this function if you need to specify param name or default service name
  • should return XXX.XXX at last
  • should call parent::getService() if can not handle

Returns

string —

service name, e.g. Default.Index

getServiceApi()

getServiceApi() : string

Get the class name of serivce

Returns

string —

class name of servcie, like: Api_XXX

getServiceAction()

getServiceAction() : string

Get the method name of servcie

Returns

string —

method name of service

genData()

genData(array  $data) : array

Generate request data

generate different request data according by different project situations, eg: only POST data accepted, or only GET data accepted, or decryped data

Parameters

array $data

origin data package

Returns

array

getAllHeaders()

getAllHeaders() : \array/false

Get header infomation

Returns

\array/false

getDataBySource()

getDataBySource(  $source) : array

Get data by source ``` |----------|---------------------| | post | $_POST | | get | $_GET | | cookie | $_COOKIE | | server | $_SERVER | | request | $_REQUEST | | header | $_SERVER['HTTP_X'] | |----------|---------------------|


Override this function if you need to get data from other source

Parameters

$source

Throws

\PhalApi_Exception_InternalServerError

Returns

array