HTTP Responses
Creating Responses
Strings & Arrays & Objects
All routes and controllers should return a response to be sent back to the client. Formidable provides several different ways to return responses. The most basic response is returning a string from a route or controller. Formidable will automatically convert the string into a full HTTP response:
- Imba
- TypeScript
Route.get '/', do 'Hello World!'
Route.get('/', (): string => 'Hello World!')
In addition to returning strings from your routes and controllers, you may also return arrays. Formidable will automatically convert the array into a JSON response:
- Imba
- TypeScript
Route.get '/', do [ 'Donald', 'Luna' ]
Route.get('/', (): Array<string> => [ 'Donald', 'Luna' ]))
You may also return an object, Formidable will automatically convert the object into a JSON response:
- Imba
- TypeScript
Route.get '/', do {
name: 'Luna'
}
Route.get('/', (): object => {
name: 'Luna'
})
Notice how we didn't include the return keyword, this is because Imba returns the last value/object by default.
Attaching Headers To Responses
You may use the setHeader method to attach a header to your response. The header method is part of the FormRequest instance:
- Imba
- TypeScript
request.setHeader('x-header', 'x-value')
request.setHeader('x-header', 'x-value')
You may also attach multiple headers by chaining the setHeader method:
- Imba
- TypeScript
request
.setHeader('x-header-1', 'x-value')
.setHeader('x-header-2', 'x-value')
.setHeader('x-header-3', 'x-value')
request
.setHeader('x-header-1', 'x-value')
.setHeader('x-header-2', 'x-value')
.setHeader('x-header-3', 'x-value')
This can be done from Route actions, Middlewares and any other place where the
FormRequestinstance is present.
Redirects
Redirect responses are instances of the Redirect class. To get started with a simple redirect, you may return Redirect instance anywhere in your application:
- Imba
- TypeScript
import { Redirect } from '@formidablejs/framework'
Redirect.to('posts/deleted')
import { Redirect } from '@formidablejs/framework'
Redirect.to('posts/deleted')
Redirecting To Named Routes
You may also redirect back to a named route by using the route method:
- Imba
- TypeScript
Redirect.route('posts.deleted')
Redirect.route('posts.deleted')
If your route has parameters, you may pass them as the second argument to the route method:
- Imba
- TypeScript
Redirect.route('posts.show', {
id: 1
})
Redirect.route('posts.show', {
id: 1
})