routes/web.py
and are extremely simple to understand. They consist of a request method and a route method. Routing is simply stating what incoming URI's should direct to which controllers.GET
request route it will look like:views.py
file if you are coming from the Django frameworkroutes/web.py
and inside the ROUTES
list. You'll see we have a route for the home page. Let's add a route for creating blogs.[email protected]
string. This means "use the blog controller's show method to render this route". The only problem here is that we don't yet have a blog controller.app/controllers
directory by default and Masonite promotes a 1 controller per file structure. 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.app/controllers
directory that looks like this:show
method we were looking for. These are called "controller methods" and are similiar to what Django calls a "view."...
means there is code in between that we are not worried about:View
class. This is what Masonite calls "Auto resolving dependency injection". If this doesn't make sense to you right now don't worry. The more you read on the more you will understand.blog
view but it does not exist yet.templates
directory. We can create a new file called templates/blog.html
.http://localhost:8000/blog
. You will see "This is a blog" in your web browser.DB_
in your .env
file. For running MySQL or Postgres you will need to have those databases setup already..env
file in the root of your project and change the DB_DATABASE
to mysql
. Also, feel free to change the DB_DATABASE
name to something else.craft auth
so we have a few new templates and controllers.databases/migrations
folder. Let's open that up and starting on line 6 we should see something that looks like:app/models/Post.py
and when we open it up it should look like:create
and update
methods later).Because of how Masonite does models, some models may rely on each other so it is typically better to perform the import inside the relationship like we did above to prevent any possibilities of circular imports.
/blog/create
and will be a simple form for creating a blog post{{ csrf_field }}
below the <form>
open tag. Masonite comes with CSRF protection so we need a token to render the hidden field with the CSRF token.auth()
is a view helper function that either returns the current user or returns None
.storage/static/
and make a blog.css
file and throw anything you like in it. For this tutorial we will make the html page slightly grey./blog/create
so we need to direct a route to our controller method. In this case we will direct it to a store
method.routes/web.py
file and create a new route. Just add this to the ROUTES
list:Post
model and create a new post with the input.request: Request
here. This is the Request
object. Where did this come from? This is the power and beauty of Masonite and your first introduction to the Service Container. The Service Container is an extremely powerful implementation as allows you to ask Masonite for an object (in this case Request
) and get that object. This is an important concept to grasp so be sure to read the documentation further.input()
method. Masonite does not discriminate against different request methods so getting input on a GET
or a POST
request are done exactly the same way by using this input
method.http://localhost:8000/blog
and create a post. This should hit the /blog/create
route with the POST
request method and we should see "post created".templates/posts.html
templates/single.html
BlogController
.show
method we will show all posts and then we will create a single
method to show a specific post.show
method to return the posts view with all the posts:http://localhost:8000/posts
route. You should see a basic representation of your posts. If you only see 1, go to http://localhost:8000/blog
to create more so we can show an individual post.@id
string. We can use this to grab that section of the URL in our controller in the next section below. This is like a route URL capture group.single
method so we show a single post.param()
method to fetch the id from the URL. Remember this key was set in the route above when we specified the @id
@slug
and then fetch it with request().param('slug')
.http://localhost:8000/post/1
route and then http://localhost:8000/post/2
and see how the posts are different.PostController
:templates/update.html
GET
route here, It would be much better to use a POST
method but for simplicity sake will assume you can create one by now. We will just add a link to our update method which will delete the post.