Response
The request and the response in Masonite work together to form a well formed response that a browser can read and render. The Response class is responsible for what data is returned and how it is formatted. In this case, a response can have cookies and headers. The Request class can also have cookies and headers. In most times, generally, when you have to fetch headers or cookies you should do so on the request class and when you set cookies and headers you should do so on the response class.
Cookies
Cookies on the response class are attached to the response and rendered as part of the response headers. Any incoming cookies you would likely fetch from the request class but you can fetch them from the response if you have a reason to:
You can also set cookies on the response:
You can also delete cookies:
Redirecting
You can redirect the user to any number of URL's.
First you can redirect to a simple URL:
You can redirect back to where the user came from:
If using the back method as part of a form request then you will need to use the back view helper as well:
You can also redirect to a route by its name:
This will find a named route users.home
like:
You may also pass parameters to a route if the URL requires it:
Finally you may also pass query string parameters to the url or route to redirect:
with_success
You can redirect by flashing a success message into session:
with_errors
You can redirect by flashing an error message or errors messages into session:
You can directly use validation errors:
with_input
You can redirect by flashing form input into session, to re-populate the form when there are errors:
Headers
Response headers are any headers that will be found on the response. Masonite takes care of all the important headers for you but there are times where you want to set you own custom headers.
Most incoming headers you want to get can be found on the request class but if you need to get any headers on the response:
Any responses you want to be returned should be set on the response class:
This will set the header on the response.
Status
You can set the status on the response simply:
Downloading
You can also very simply download assets like images, PDF's or other files:
This will set all the proper headers required and render the file in the browser.
When setting the name, the file extension will be picked up from the file type. This example will download the invoice as the name
invoice-2021-01.pdf
If you want to force download it, or automatically download the file when the response in rendered, you can add a force
parameter and set it to True
:
Last updated