Requests
Introduction
The Request class is initialized on every request by the framework and passed into the IOC container. This allows all parts of the framework to be resolved by the IOC container and auto inject any dependencies they need.
Getting Started
The request
object is bound into the IOC container on every request. This takes the WSGI environment variables generated by your WSGI server as a parameter. This is done by Masonite. This Request
class is initialized inside the bootstrap/start.py
file. We grab this request object by simply passing in Request
into our controller method.
def show(self, Request):
# request is the instance of Request
pass
Usage
The Request
has several helper methods attached to it in order to interact with various aspects of the request.
In order to get the current request input variables such as the form data during a POST
request or the query string during a GET
request looks like:
def show(self, Request):
Request.input('username')
NOTE: There is no difference between GET
and POST
when it comes to getting input data. They are both retrieved through this .input()
method so there is no need to make a distinction if the request is GET
or POST
In order to get all the request input variables such as input data from form data or from a query string. This will return all the available request input variables for that request as a dictionary.
def show(self, Request):
return Request.all()
To check if a request input data exists:
def show(self, Request):
return Request.has('variable')
To get the request parameter retrieved from the url. This is used to get variables inside: /dashboard/@firstname
for example.
def show(self, Request):
return Request.param('firstname')
You may also set a cookie in the browser. The below code will set a cookie named key
to the value of value
def show(self, Request):
return request.cookie('key', 'value')
You can get all the cookies set from the browser
def show(self, Request):
return request.get_cookies()
You can get a specific cookie set from the browser
def show(self, Request):
return Request.get_cookie('key')
Get the current user from the request. This requires the LoadUserMiddleware
middleware which is in Masonite by default but commented out. This middleware should only be used if you have a valid database connection. This will return an instance of the current user.
def show(self, Request):
return Request.user()
You can specify a url to redirect to
def show(self, Request):
return Request.redirect('/home')
If the url contains http
than the route will redirect to the external website
def show(self, Request):
return Request.redirect('http://google.com')
You can redirect to a named route
def show(self, Request):
return Request.redirectTo('dashboard')
You can also go back to a named route specified from the form parameter back
. This will get the request input named back
and redirect to that named route. This is great if you want to redirect the user to a login page and then back to where they came from. Just remember during your form submission that you need to supply a back
input.
def show(self, Request):
return Request.back()
This is equivalent to:
def show(self, Request):
return Request.redirectTo(request.input('back'))
You can also specify the input parameter that contains the named route
def show(self, Request):
return Request.back('redirect')
Sometimes your routes may require parameters passed to it such as redirecting to a route that has a url like: /url/@firstname:string/@lastname:string
. For this you can use the send
method. Currently this only works with named routes.
def show(self, Request):
return Request.back().send({'firstname': 'Joseph', 'lastname': 'Mancuso'})
return Request.redirectTo('dashboard').send({'firstname': 'Joseph', 'lastname': 'Mancuso'})
You can load a specific secret key into the request by using:
Request.key(key)
This will load a secret key into the request which will be used for encryptions purposes throughout your Masonite project. Note that by default, the secret key is pulled from your configuration file so you do NOT need to supply a secret key, but the option is there if you need to change it