Response
The response can take various forms, including strings, maps, models, serializers, lists of models, and various types of exceptions.
/// string
class BlogController {
index(DoxRequest req) {
return 'pong';
}
}
/// exception
class BlogController {
index(DoxRequest req) {
return InternalErrorException();
}
}
/// map
class BlogController {
index(DoxRequest req) {
return {"message" : "hello world!"};
}
}
/// Model
class BlogController {
index(DoxRequest req) {
Admin admin = await Admin().find(1)
return admin;
}
}
/// List<Model>
class BlogController {
index(DoxRequest req) {
List admins = await Admin().all()
return admins;
}
}
/// List<Model>
class PdfController {
download(DoxRequest req) {
DownloadableFile file = await Storage().download('filename.pdf');
return file;
}
}
/// List<Model>
class PdfController {
download(DoxRequest req) {
StreamFile file = await Storage().stream('filename.pdf');
return file;
}
}
Info
Models and lists of models will be automatically converted into JSON format and arrays of JSON.
With
Headers
return response()
.header('Authorization', 'Bearer xxxxxx')
.header('ContentType', 'application/json');
return response().withHeaders({
'Authorization' : 'Bearer xxxxxx',
'ContentType', 'application/json'
});
Status code
return response().statusCode(401);
Content type
return response().contentType(ContentType.json);
Stream
StreamFile file = await Storage().stream('filename.jpg');
return response().stream(file.stream).contentType(file.contentType);
Cookie
var cookie = DoxCookie('key', 'value');
return response().cookie(cookie);
controllerMethod(DoxRequest req) {
cookie = req.cookie(key)
}
Encrypt / Decrypt
By default, a cookie's value is encrypted and decrypted using the APP_KEY
. You can opt to disable encryption by setting encrypt: false
when setting a cookie and disable decryption by setting decode: false
when retrieving a cookie.