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.
Like most parts of Masonite, you can scaffold a controller with a craft command:
This will create a controller in app/http/controllers
that looks like:
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.
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:
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
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:
This will create that file for us.
If we put some text in this file like:
and then run the server
and open up localhost:8000/blog
, we will see "This is a blog"
In the next part we will start designing our blog application