In this episode, we're cleaning up how our app handles HTTP requests and responses. We've already created this App
class to take care of the high-level lifecycle of our application, and now we're going to actually put it to use so everything isn't just thrown together in our bootstrap file.
First, we set up the App
class to resolve the request and router out of our dependency injection container, so we don't need to manually pass them around anymore. This makes things a lot tidier and keeps our main entry file less cluttered. We add a helper method to extract the router from the app when we need to register routes.
Next, we move the core logic for dispatching a request and emitting a response into the run
method of the app. This means the bootstrap file can just create the app, register some routes, and call run
. Much easier to follow!
We also take a first step toward error handling. Now, when we emit a response, we do so inside a try/catch block, catching any exceptions (like if a route isn't found) and preparing to handle them gracefully—like returning a proper 404 page. Right now, we just re-throw the exception, but soon we'll improve that.
At the end of the episode, we recap how the request flows from the client into our app, gets routed, creates a response, and sends it back out. It's a good mental model for how these pieces fit together. Finally, we set ourselves up for the next episode, where we'll clean up route registration so it's not all stuck in bootstrap anymore.
So, after this episode, the app structure is more organized, easier to maintain, and is ready for the next steps in making it a proper, maintainable framework!