Are you an organized developer who keeps code simple and clean? Are you a good controller? Are you following the 7 RESTful Methods consistently
Let me congratulate your efforts to implement correct M-V-C practices if the majority of your answers are YES. If you answered NO to most of these questions, then you need to change your habits. It can be difficult to understand complicated codes and cause serious headaches when trying to debug programming errors.
The Model-View-Controller Framework
In programming, a Controller is one of the main components of Model-View-Controller Framework or M-V-C Structure. It is responsible for directing the flow of data between Models and Views to maintain order and efficiency. Developers work on many features. Controllers can become cluttered and full of junk due to developer’s inability to implement a new feature or lack of knowledge about how to use Controllers properly.
Let’s face it, combining “relevant” methods in one controller has many advantages. It is much faster than creating a new controller class. Understanding the 7 RESTful Methods, which Roy Fielding introduced in 2000 as part of his doctoral dissertation, is a great asset when testing APIs. Each method has its own function. These methods will help you save time and effort if you apply them to your own work. These methods will make your code more readable for others, and can make API implementations easier.
7 RESTful Methods
GET- returns a selection of resources
NEW- Returns a form to create new resources
SHOW- Returns an existing resource
POST- adds a new resource into the database
EDIT- Returns a form to modify an existing resource
PUT- Updates an existing resource in the database
DELETE- Deletes an existing resource from the database
The 7 RESTful methods are available by default and are commonly used when browsing the internet. When you visit a website, for example, you will receive a representation of the resource via either a GET or NEW, SHOW or DELETE endpoint or method. When you submit something via form submissions to a website, you are sending the representation to the POST, DELETE, or PUT endpoints or methods.
Notice: While I am using MEAN stack for the code examples, you are free to use any MVC framework of choice.
Imagine that you have a Travel Expense Tracker App. To understand how it works, we will create a TravelsController.
You’re familiar with the process. These requests should be returned or processed in specific ways. What if we want an expense added to a specific travel? Should we add a POST /travels/:id/add_expense url, and point it to an add_expense method in TravelsController? What about cancelling a travel expense? Or, show us which travels were cancelled.
Here are some methods that you can add to your controller.
Although it is okay to add these methods to the TravelsController it is not recommended. However, it is important that you do not create more methods than the original public methods. These customized methods can be managed by placing them in their controllers.
In the above example, there are two ways to deal with travel expenses. In this case, consider creating a new controller class and name it TravelExpensesController:
You have two options when it comes to showing all canceled trips:
1. Use filters in TravelsController to implement the GET method. You’ll hear more about it in my next blog, so stay tuned!
2. Create a CanceledTravelsController.
Option 2: You can use the GET method for all canceled travels and to handle any actions related to cancelling travels. This approach does not change the 7 RESTful methods. This is true for pending trips, as well. To organize multiple controllers belonging to the same category, it would be better to create a separate folder to store all travel-related controller files.
But don’t get too excited! This is not the only solution to bulky controllers. This approach is not required for every situation. If you are planning on implementing new features and feel that the methods should have their own controller, then this is the right approach.
If you’re interested in learning more about web development, visit our website:https://www.codingdojo.com/
https://en.wikipedia.org/wiki/Representational_state_transfer https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller https://whatis.techtarget.com/definition/model-view-controller-MVC https://en.wikipedia.org/wiki/Create,_read,_update_and_delete https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods
Written by: John Oliver Rosales 09/21/18