Part 2 - Creating Our First Controller

Getting Started

All controllers are located in the app/http/controllers directory and Masonite promotes 1 controller per file. This has proven efficient for larger application development because most developers use text editors with advanced search features such as Sublime, VSCode or Atom. Switching between classes in this instance is simple and promotes faster development. It's easy to remember where the controller exactly is because the name of the file is the controller.

You can of course move controllers around wherever you like them but the craft command line tool will default to putting them in separate files.

Creating Our First Controller

Like most parts of Masonite, you can scaffold a controller with a craft command:

terminal
$ craft controller Blog

This will create a controller in app/http/controllers that looks like:

app/http/controller/BlogController.py
class BlogController:
    ''' Class Docstring Description '''

    def show(self):
        pass

Simple enough, right? You'll notice we have a show method. These are called "controller methods" and are similiar to what Django calls a "view."

Notice we now have our show method that we specified in our route.

Returning a View

We can return a view from our controller. A view in Masonite are html files. They are not Python objects themselves like other Python frameworks. Views are what the users will see. We can return a view by using the view() function:

app/http/controllers/BlogController.py
def show(self):
    return view('blog')

Notice here we didn't import anything. Masonite comes with several helper functions that act like built in Python functions. These helper functions make developing with Masonite really efficient.

You can learn more about helper functions in the Helper Functions documentation

Creating Our View

You'll notice now that we are returning the blog view but it does not exist.

All views are in the resources/templates directory. We can create a new file called resources/templates/blog.html or we can use another craft command:

terminal
$ craft view blog

This will create that file for us.

If we put some text in this file like:

resources/templates/blog.html
This is a blog

and then run the server

terminal
$ craft serve

and open up localhost:8000/blog, we will see "This is a blog"

In the next part we will start designing our blog application

Last updated