上QQ阅读APP看书,第一时间看更新
Building a simple router for a user module
Routing is one of the important aspects in advanced application development. We need to figure out ways of building custom routes for specific functionalities. In this scenario, we will create a custom router to handle all the user-related functionalities of our application.
Let's list the requirements for building a router:
- All the user-related functionalities should go through a custom URL, such as http://www.example.com/user
- Registration should be implemented at http://www.example.com/user/register
- Login should be implemented at http://www.example.com/user/login
- Activation should be implemented at http://www.example.com/user/activate
Make sure to set up your permalinks structure to post names for the examples in this book. If you prefer a different permalinks structure, you will have to update the URLs and routing rules accordingly.
As you can see, the user section is common for all the functionalities. The second URL segment changes dynamically based on the functionality. In MVC terms, the user acts as the controller and the next URL segment (register, login, and activate) acts as the action. Now, let's see how we can implement a custom router for the given requirements.