Summary
In this episode, we explore controllers as the traffic directors of a web application that receive requests, call models for data, and pass results to views. We start by building a simple controller using Go's standard library, then refactor it using the Echo framework to reduce boilerplate code. Echo provides helpful abstractions through its context object, making it easier to handle requests, extract route parameters, and manage errors, while maintaining the same fundamental pattern of receiving requests, calling models, and rendering views.
Transcript
Now we have introduced what our model and what our view is. The final piece we are missing is the controller. Controllers are like the traffic directors of your app. They receive incoming requests and then they figure out where to route it and what to return as a response. This often means calling a model, getting some data and then passing it on to our view. Now again, the standard library has actually very good support for everything we need when it comes to web development. This means that we could write a simple controller using just the Go standard library. And the way we would do that is just to say func and let's say home controller. This is going to take in our HTTP response writer, is going to take in our HTTP request. And then inside, let's just say we grab all of our articles from our model, models.findArticles, we're going to pass the context. And then if we have an error, we are going to say HTTP error. And we're going to pass the response writer. We're going to say something went wrong. Not really the best error message, but it gets the point across. And we also need a HTTP status code, so let's just say server error and then just a return here. But if nothing goes wrong, then we simply call our views.home. We pass the articles, then we call render because this is a method exposed by Templ where we can pass the context and then again, the response writer. So here the controller function home controller takes in a request, asks the model for some data, and then it renders the appropriate view. This works and you could totally continue doing this, but as your app grows, managing all these handlers using the net/HTTP package can get quite repetitive. You often need a bit of boilerplate for error handling. ECHO gives us a clean and structured way to write controllers, but also write routes, as you'll see in one of the upcoming episodes. If we were to express this controller, but using ECHO instead, we would get rid of these two arguments and simply just have one argument, which is going to be the ECHO context, and we need to add this to our go.mod file. For example, let's say returning some HTML, or we could return JSON, but for now, let's just return the error. Finally, we can say return views.home and just update this to use the request, and then the response. There we go, and we have all the arguments that Templ expects. So, ECHO provides us with a lot of helper methods. It removes a lot of the repetitiveness through the ECHO context. Now, let me just do this, but for the article views, we can render both the home page, but also the article page. All right, and to add the article controller, we can just grab everything we have here, and let's just call it article and expose it, because we're already in the controller package, and do the same for home here, there we go. Now, remember when we added the anchor tag in the last episode about views, so that we could actually click an article and be passed in the article's ID. Well, the way we can grab that in the controller is to use a helper method from ECHO. So, we're going to say ID or error, and we're going to receive a string that we technically need to be int64. So, we're going to wrap all of this in a string conversion, say strconv, okay, there we go, and it's imported. So, this simply just accepts a string, and then returns an integer. And then we can say c.Param, because it's going to be a parameter we're going to pass. It's going to be called ID, and this will make a bit more sense when we get to the router and routes layer. But for now, just stick with me. This will get passed through the view, right? Again, let's just return an error if there is one. And now we need to call findArticle. That needs to get our ID in the form of an int64. So, let's just wrap our ID in this. And now we are not rendering home anymore. We are rendering article. And let's just call this one article. So, here we are using one of the helper methods exposed by Echo to easily extract the ID that gets passed as a route parameter. Then we convert it to our integer. And then finally, we convert it to an int64 so that our model can go into the database and look up this article. So, notice how the structure is very similar. The controller takes in the request, it calls some models, and then passes the results onto the view. However, Echo gives us some nice abstractions. We get the request and response wrapped in the Echo context. We have an easier way to work with something like path parameters, but could also be query strings. It plays very nicely with middleware, with error handling, and also routing, which we will see in one of the upcoming episodes.
