diff --git a/developer_manual/app/controllers.rst b/developer_manual/app/controllers.rst index 5098f7bf2..3a0b2aafa 100644 --- a/developer_manual/app/controllers.rst +++ b/developer_manual/app/controllers.rst @@ -322,6 +322,7 @@ By default there is only a responder for JSON but more can be added easily: namespace OCA\MyApp\Controller; use \OCP\AppFramework\Controller; + use \OCP\AppFramework\Http\DataResponse; class PageController extends Controller { @@ -329,7 +330,15 @@ By default there is only a responder for JSON but more can be added easily: // XMLResponse has to be implemented $this->registerResponder('xml', function($value) { - return new XMLResponse($value); + if ($value instanceof DataResponse) { + return new XMLResponse( + $value->getData(), + $value->getStatus(), + $value->getHeaders() + ); + } else { + return new XMLResponse($value); + } }); return array('test' => 'hi'); @@ -339,6 +348,30 @@ By default there is only a responder for JSON but more can be added easily: .. note:: The above example would only return XML if the **format** parameter was *xml*. If you want to return an XMLResponse regardless of the format parameter, extend the Response class and return a new instance of it from the controller method instead. +Because returning values works fine in case of a success but not in case of failure that requires a custom HTTP error code, you can always wrap the value in a **DataResponse**. This works for both normal responses and error responses. + +.. code-block:: php + + 'not found!'), Http::STATUS_NOT_FOUND); + } + } + + } + + Templates --------- A :doc:`template ` can be rendered by returning a TemplateResponse. A TemplateResponse takes the following parameters: