PHP Class Zend\Mvc\Controller\AbstractRestfulController

Inheritance: extends AbstractController
Show file Open project: zendframework/zend-mvc Class Usage Examples

Protected Properties

Property Type Description
$contentTypes array
$customHttpMethodsMap array Map of custom HTTP methods and their handlers
$eventIdentifier {@inheritDoc}
$identifierName string Name of request or query parameter containing identifier
$jsonDecodeType integer | boolean The flags in Zend\Json\Json::decode are integers, but when evaluated in a boolean context map to the flag passed as the second parameter to json_decode(). As such, you can specify either the Zend\Json\Json constant or the boolean value. By default, starting in v3, we use the boolean value, and cast to integer if using Zend\Json\Json::decode. Default value is boolean true, meaning JSON should be cast to associative arrays (vs objects). Override the value in an extending class to set the default behavior for your class.

Public Methods

Method Description
addHttpMethodHandler ( string $method, Callable $handler ) : AbstractRestfulController Register a handler for a custom HTTP method
create ( mixed $data ) : mixed Create a new resource
delete ( mixed $id ) : mixed Delete an existing resource
deleteList ( $data ) : mixed Delete the entire resource collection
dispatch ( Zend\Stdlib\RequestInterface $request, Zend\Stdlib\ResponseInterface $response = null ) : mixed | Zend\Stdlib\ResponseInterface Dispatch a request
get ( mixed $id ) : mixed Return single resource
getIdentifierName ( ) : string Retrieve the route match/query parameter name containing the identifier
getList ( ) : mixed Return list of resources
head ( null | mixed $id = null ) : mixed Retrieve HEAD metadata for the resource
notFoundAction ( ) : array Basic functionality for when a page is not available
onDispatch ( MvcEvent $e ) : mixed Handle the request
options ( ) : mixed Respond to the OPTIONS method
patch ( $id, $data ) : array Respond to the PATCH method
patchList ( mixed $data ) : mixed Modify a resource collection without completely replacing it
processPostData ( Zend\Stdlib\RequestInterface $request ) : mixed Process post data and call create
replaceList ( mixed $data ) : mixed Replace an entire resource collection
requestHasContentType ( Zend\Stdlib\RequestInterface $request, string | null $contentType = '' ) : boolean Check if request has certain content type
setIdentifierName ( string $name ) : self Set the route match/query parameter name containing the identifier
update ( mixed $id, mixed $data ) : mixed Update an existing resource

Protected Methods

Method Description
getIdentifier ( Zend\Router\RouteMatch $routeMatch, Zend\Stdlib\RequestInterface $request ) : false | mixed Retrieve the identifier, if any
jsonDecode ( $string ) : mixed Decode a JSON string.
processBodyContent ( mixed $request ) : object | string | array Process the raw body content

Method Details

addHttpMethodHandler() public method

This method allows you to handle arbitrary HTTP method types, mapping them to callables. Typically, these will be methods of the controller instance: e.g., array($this, 'foobar'). The typical place to register these is in your constructor. Additionally, as this map is checked prior to testing the standard HTTP methods, this is a way to override what methods will handle the standard HTTP methods. However, if you do this, you will have to retrieve the identifier and any request content manually. Callbacks will be passed the current MvcEvent instance. To retrieve the identifier, you can use "$id = $this->getIdentifier($routeMatch, $request)", passing the appropriate objects. To retrieve the body content data, use "$data = $this->processBodyContent($request)"; that method will return a string, array, or, in the case of JSON, an object.
public addHttpMethodHandler ( string $method, Callable $handler ) : AbstractRestfulController
$method string
$handler Callable
return AbstractRestfulController

create() public method

Create a new resource
public create ( mixed $data ) : mixed
$data mixed
return mixed

delete() public method

Delete an existing resource
public delete ( mixed $id ) : mixed
$id mixed
return mixed

deleteList() public method

Not marked as abstract, as that would introduce a BC break (introduced in 2.1.0); instead, raises an exception if not implemented.
public deleteList ( $data ) : mixed
return mixed

dispatch() public method

If the route match includes an "action" key, then this acts basically like a standard action controller. Otherwise, it introspects the HTTP method to determine how to handle the request, and which method to delegate to.
public dispatch ( Zend\Stdlib\RequestInterface $request, Zend\Stdlib\ResponseInterface $response = null ) : mixed | Zend\Stdlib\ResponseInterface
$request Zend\Stdlib\RequestInterface
$response Zend\Stdlib\ResponseInterface
return mixed | Zend\Stdlib\ResponseInterface

get() public method

Return single resource
public get ( mixed $id ) : mixed
$id mixed
return mixed

getIdentifier() protected method

Attempts to see if an identifier was passed in either the URI or the query string, returning it if found. Otherwise, returns a boolean false.
protected getIdentifier ( Zend\Router\RouteMatch $routeMatch, Zend\Stdlib\RequestInterface $request ) : false | mixed
$routeMatch Zend\Router\RouteMatch
$request Zend\Stdlib\RequestInterface
return false | mixed

getIdentifierName() public method

Retrieve the route match/query parameter name containing the identifier
public getIdentifierName ( ) : string
return string

getList() public method

Return list of resources
public getList ( ) : mixed
return mixed

head() public method

Not marked as abstract, as that would introduce a BC break (introduced in 2.1.0); instead, raises an exception if not implemented.
public head ( null | mixed $id = null ) : mixed
$id null | mixed
return mixed

jsonDecode() protected method

Uses json_decode by default. If that is not available, checks for availability of Zend\Json\Json, and uses that if present. Otherwise, raises an exception. Marked protected to allow usage from extending classes.
protected jsonDecode ( $string ) : mixed
return mixed

notFoundAction() public method

Basic functionality for when a page is not available
public notFoundAction ( ) : array
return array

onDispatch() public method

Handle the request
public onDispatch ( MvcEvent $e ) : mixed
$e Zend\Mvc\MvcEvent
return mixed

options() public method

Typically, set the Allow header with allowed HTTP methods, and return the response. Not marked as abstract, as that would introduce a BC break (introduced in 2.1.0); instead, raises an exception if not implemented.
public options ( ) : mixed
return mixed

patch() public method

Not marked as abstract, as that would introduce a BC break (introduced in 2.1.0); instead, raises an exception if not implemented.
public patch ( $id, $data ) : array
$id
$data
return array

patchList() public method

Not marked as abstract, as that would introduce a BC break (introduced in 2.2.0); instead, raises an exception if not implemented.
public patchList ( mixed $data ) : mixed
$data mixed
return mixed

processBodyContent() protected method

If the content-type indicates a JSON payload, the payload is immediately decoded and the data returned. Otherwise, the data is passed to parse_str(). If that function returns a single-member array with a empty value, the method assumes that we have non-urlencoded content and returns the raw content; otherwise, the array created is returned.
protected processBodyContent ( mixed $request ) : object | string | array
$request mixed
return object | string | array

processPostData() public method

Process post data and call create
public processPostData ( Zend\Stdlib\RequestInterface $request ) : mixed
$request Zend\Stdlib\RequestInterface
return mixed

replaceList() public method

Not marked as abstract, as that would introduce a BC break (introduced in 2.1.0); instead, raises an exception if not implemented.
public replaceList ( mixed $data ) : mixed
$data mixed
return mixed

requestHasContentType() public method

Check if request has certain content type
public requestHasContentType ( Zend\Stdlib\RequestInterface $request, string | null $contentType = '' ) : boolean
$request Zend\Stdlib\RequestInterface
$contentType string | null
return boolean

setIdentifierName() public method

Set the route match/query parameter name containing the identifier
public setIdentifierName ( string $name ) : self
$name string
return self

update() public method

Update an existing resource
public update ( mixed $id, mixed $data ) : mixed
$id mixed
$data mixed
return mixed

Property Details

$contentTypes protected property

protected array $contentTypes
return array

$customHttpMethodsMap protected property

Map of custom HTTP methods and their handlers
protected array $customHttpMethodsMap
return array

$eventIdentifier protected property

{@inheritDoc}
protected $eventIdentifier

$identifierName protected property

Name of request or query parameter containing identifier
protected string $identifierName
return string

$jsonDecodeType protected property

The flags in Zend\Json\Json::decode are integers, but when evaluated in a boolean context map to the flag passed as the second parameter to json_decode(). As such, you can specify either the Zend\Json\Json constant or the boolean value. By default, starting in v3, we use the boolean value, and cast to integer if using Zend\Json\Json::decode. Default value is boolean true, meaning JSON should be cast to associative arrays (vs objects). Override the value in an extending class to set the default behavior for your class.
protected int|bool $jsonDecodeType
return integer | boolean