\OCP\AppFrameworkOCSController

Base class to inherit your controllers from that are used for RESTful APIs

Summary

Methods
Properties
Constants
__construct()
preflightedCors()
getResponderByHTTPHeader()
buildResponse()
params()
getParams()
method()
getUploadedFile()
env()
cookie()
render()
No public properties found
No constants found
registerResponder()
$appName
$request
N/A
buildOCSResponse()
$corsMethods
$corsAllowedHeaders
$corsMaxAge
$responders
N/A

Properties

$appName

$appName : string

app name

Type

string

$request

$request : \OCP\IRequest

current request

Type

\OCP\IRequest

$corsMethods

$corsMethods : 

Type

$corsAllowedHeaders

$corsAllowedHeaders : 

Type

$corsMaxAge

$corsMaxAge : 

Type

$responders

$responders : array

Type

array

Methods

__construct()

__construct(string  $appName, \OCP\IRequest  $request, string  $corsMethods = 'PUT, POST, GET, DELETE, PATCH', string  $corsAllowedHeaders = 'Authorization, Content-Type, Accept', integer  $corsMaxAge = 1728000) 

constructor of the controller

Parameters

string $appName

the name of the app

\OCP\IRequest $request

an instance of the request

string $corsMethods

comma separated string of HTTP verbs which should be allowed for websites or webapps when calling your API, defaults to 'PUT, POST, GET, DELETE, PATCH'

string $corsAllowedHeaders

comma separated string of HTTP headers which should be allowed for websites or webapps when calling your API, defaults to 'Authorization, Content-Type, Accept'

integer $corsMaxAge

number in seconds how long a preflighted OPTIONS request should be cached, defaults to 1728000 seconds

preflightedCors()

preflightedCors() 

This method implements a preflighted cors response for you that you can link to for the options request

getResponderByHTTPHeader()

getResponderByHTTPHeader(string  $acceptHeader) : string

Parses an HTTP accept header and returns the supported responder type

Parameters

string $acceptHeader

Returns

string —

the responder type

buildResponse()

buildResponse(mixed  $response, string  $format = 'json') : \OCP\AppFramework\Http\Response

Serializes and formats a response

Parameters

mixed $response

the value that was returned from a controller and is not a Response instance

string $format

the format for which a formatter has been registered

Throws

\DomainException

if format does not match a registered formatter

Returns

\OCP\AppFramework\Http\Response

params()

params(string  $key, string  $default = null) : mixed

Lets you access post and get parameters by the index

Parameters

string $key

the key which you want to access in the URL Parameter placeholder, $_POST or $_GET array. The priority how they're returned is the following:

  1. URL parameters
  2. POST parameters
  3. GET parameters
string $default

If the key is not found, this value will be returned

Returns

mixed —

the content of the array

getParams()

getParams() : array

Returns all params that were received, be it from the request (as GET or POST) or through the URL by the route

Returns

array —

the array with all parameters

method()

method() : string

Returns the method of the request

Returns

string —

the method of the request (POST, GET, etc)

getUploadedFile()

getUploadedFile(string  $key) : array

Shortcut for accessing an uploaded file through the $_FILES array

Parameters

string $key

the key that will be taken from the $_FILES array

Returns

array —

the file in the $_FILES element

env()

env(string  $key) : array

Shortcut for getting env variables

Parameters

string $key

the key that will be taken from the $_ENV array

Returns

array —

the value in the $_ENV element

cookie()

cookie(string  $key) : array

Shortcut for getting cookie variables

Parameters

string $key

the key that will be taken from the $_COOKIE array

Returns

array —

the value in the $_COOKIE element

render()

render(string  $templateName, array  $params = array(), string  $renderAs = 'user', array<mixed,string>  $headers = array()) : \OCP\AppFramework\Http\TemplateResponse

Shortcut for rendering a template

Parameters

string $templateName

the name of the template

array $params

the template parameters in key => value structure

string $renderAs

user renders a full page, blank only your template admin an entry in the admin settings

array<mixed,string> $headers

set additional headers in name/value pairs

Returns

\OCP\AppFramework\Http\TemplateResponse

containing the page

registerResponder()

registerResponder(string  $format, \Closure  $responder) 

Registers a formatter for a type

Parameters

string $format
\Closure $responder

buildOCSResponse()

buildOCSResponse(string  $format, array|\OCP\AppFramework\Http\DataResponse  $data) 

Unwrap data and build ocs response

Parameters

string $format

json or xml

array|\OCP\AppFramework\Http\DataResponse $data

the data which should be transformed