Controllers
Controllers are a place where most of your business logic will be. Controllers are where you put the responses you see in the web browser. Responses can be dictionaries, lists, views or any class that can render a response.

Introduction

You may use a craft command to create a new basic controller or simply make a new controller manually. Controllers are classes with methods that are mapped to a route.
Your route may look something like this:
1
Route.get('/', '[email protected]')
Copied!
In this case this route will call the WelcomeController classes show method.
To create a basic controller via a craft command simply run:
1
$ python craft controller Welcome
Copied!
This will create a new controller class to get you setup quickly. This controller class will look like a basic class like this:
1
from masonite.controllers import Controller
2
from masonite.views import View
3
4
5
class WelcomeController(Controller):
6
def show(self, view: View):
7
return view.render("")
Copied!
You may start building your controller out and adding the responses you need.
Note that the controllers inherit Masonite base Controller class. This is required for Masonite to pick up your controller class in routes.

Dependency Injection

Your controller's constructor and controller methods are resolved by Masonite's Service Container. Because of this, you can simply typehint most of Masonite's classes in either the constructor or the methods:
1
def __init__(self, request: Request):
2
self.request = request
3
4
#..
5
6
def show(self, request: Request):
7
return request.param('1')
Copied!
Read more about the benefits of the Service Container.

Responses

Controllers can have different response types based on what you need to return.

JSON

If you want to return a JSON response you can return a dictionary or a list:
1
def show(self):
2
return {"key": "value"}
Copied!
This will return an application/json response.

Strings

You can return strings:
1
def show(self):
2
return "welcome"
Copied!

Views

If you want to return a view you can resolve the view class and use the render method:
1
def show(self, view: View):
2
return view.render("views.welcome")
Copied!

Models

If you are using Masonite ORM, you can return a model directly:
1
from app.User import User
2
#..
3
4
def show(self, response: Response):
5
return User.find(1)
Copied!

Redirects

If you want to return a redirect you can resolve the response class and use the redirect method:
1
def show(self, response: Response):
2
return response.redirect('/home')
Copied!

Other

You can return any class that contains a get_response() method. This method needs to return any one of the above response types.

Request Parameters

If you had a parameter in your route, you may fetch it by specifying the parameter in the controller's method:
1
Route.get('/users/@user_id', '[email protected]')
Copied!
Since the id parameter is in the route we can fetch the value of this parameter by specifying it in the controller method signature:
1
def show(self, user_id):
2
return User.find(user_id)
Copied!
Another way to fetch route parameters is through the request class:
1
from masonite.request import Request
2
3
def show(self, request: Request):
4
return User.find(request.param('user_id'))
Copied!

Controller Locations

Masonite will be able to pick up controllers (inheriting Controller class) using string binding in the registered controllers locations.
The default registered controllers location is app/controllers and is defined in project Kernel.py configuration file:
1
self.application.bind("controllers.location", "app/controllers")
2
# ...
3
Route.set_controller_locations(self.application.make("controllers.location"))
Copied!

Setting locations

You can override the registered controllers location in Kernel.py file by editing the default binding controllers.location.

Adding locations

You can multiple additional controller locations with add_controller_locations:
1
from masonite.routes import Route
2
3
Route.add_controller_locations("app/http/controllers", "other_module/controllers")
Copied!
The best place to do this is in your Kernel.py file in the register_routes() method.
You should do it before registering routes, else registering routes will fail as Masonite will fail to resolve controller classes.