Routing in MVC Architecture
The router maps incoming HTTP requests (GET, POST, PUT, DELETE) to specific controller functions. It acts as the intermediary between request paths and the appropriate controller logic.
Centralized Route Definitions
A dedicated routes package is created to define application paths in a single location. This prevents hard-coded strings and ensures consistency when updating routes.
Dynamic URL Parameters
Routes such as /articles/:id use parameters to extract values (e.g., article IDs) from the URL. These parameters are programmatically replaced when generating links in views.
Refactoring Views to Use Route Constants
Hard-coded paths in views are replaced with references to centralized route variables. This improves maintainability and reduces duplication.
Router Package Setup with Echo
A router package is implemented to configure the Echo framework. It defines a router struct holding the Echo instance and registers routes to their corresponding controllers.
Server Integration
The router returns a configured handler that is later used to initialize and run the server. This structure supports future expansion, including middleware and logging configuration.
we have seen how controllers receive a request pass them on to a model and then pass a response
to our view but we still need a way to route urls to the right controller function and this is where
this rather router comes in we need a way to match an incoming http request to a specific controller
for a given path and method so this is get request post request or put request right
technically also a delete request we have already created a route in our home view that will direct
the user to the article controller but now i want to formalize this a bit so we have a specific place
where we reference and create routes before we create the router let's quickly make a dedicated
routes package this package will contain all the path for our application and by centralizing these
avoid scatterings magic strings throughout the code base and we don't have one version in one
place and another version in another and then wonder why why we don't hit the correct controller
so in the router slash routes folder i'm going to create a file called routes.go
let's say routes i'm going to create two variables i'm going to create home
page set equal to r the root part is just a slash and then article article page
it's going to be slash articles and then an id parameter so again home is just the root article
page is defined with the colon id parameter which just research allows the controller to extract the
id from the url now we can also go ahead and replace the the url or the path that we put in our
home home view so that we we will be able to use the route that we have created in our home view
so that we we represent this this route in our in our view so go into home here
and down here we have this hard-coded path where instead we can say let's say strings replace
and then why doesn't it want to let me just clear out everything here we don't need fmt
so let's say strings right replace and we're going to replace in our routes
dot article page we are going to replace the colon id with and now we need to convert our
our integer so is there a e e t o r and int and article id
there we go finally replace that with
with a one and then i'm screw this up a bit so we still need this import so we need to import
our routes from the routes package
and we need to import the string conversion package
there we go and now we reference reference a specific place for this for this path if we
were to update it for any reason we would only have to be to update it in in one place now we
can actually create the router package and this is where we configure echo via routes to a controller
it's middleware it's logging and all these kind of things so in the router folder i'm going to create
a file called router.go in package router and we're going to define our router struct
which will hold field called handler which is just a pointer to echo then we need a new function
to actually create this router struct and it's just going to return the router and here we're
going to say e which is just uh it's just how echo does it in the documentation so that's why i always
just default to using or calling the variable e then we can create our two routes we have the
routes to home page which is controllers home then we have the route to our article page which is
routes article page so controllers and article don't need to call the function and then finally
the route to our article and then pass the the echo new instance but we have registered the the routes
on so super simple um we will continuously add to this as our our scope expands for now we just need
to be able to register some routes and get back our router so that we can actually use our
our router to configure our server with so we will actually use this handler that holds whatever we
we configure our router to be when we set up our server.