One way to keep full control of an application is to have a map of all the URLs your whole application will have. While building your booking application, it is a recommended practice to define all of your URLS in the form of routes. Routes are the associations between a URL and a controller method. Explore how to make a route, some variations of routes, and how to associate them with a controller method.
- [Instructor] Routes are the URL entry points of our application. They define where and what the application should call once the user types a URL or clicks on a link. It also allows us to have URLs with intent, like we see here, with clients/book/4. Instead of having a cryptic URL like this one. So let's define a route as a map from the defined URL to a controller action or method. In this example, we already have the controllers defined for our application.
So let's open up our files on the desktop. Exercise files, chapter two, 02_02, and we're going to drag the start folder into the Visual Studio code. So let's define our route as a map from the defined URL to a controller action or method. In this example, we already have the controllers defined for our application. AdminController, in charge of any admin operations, the ClientController, which will be in charge of any operation related to the guest, and the ReservationController, which will manage the booking.
So now, let's couple a URL, in this case, the root URL, with the index method of the AdminController, which will only show the application's initial page. We'll be using a method for routing called annotation, which is a default for Symfony. If your server is already running, then you can skip this. I'm going to start mine right now. As we go through our course, it's a good practice to clear our cache every now and then. So we'll clear our cache with this command, php, bin/console, cache: clear.
And now let's start our server with php, bin/console, server:run. And now let's open it up in our browser. Localhost, port 8,000, and we get the Symfony default page. This is because we have not set up any routes. So let's create our first route. We'll be using a method for routing called annotation, which is the default for Symfony. We do this by typing slash, star, star, like a comment, and inside the comment we add @Route, and inside the parentheses, we add the route that we'll be typing.
Let's save and refresh our app, and we get this ugly error. This is because, in Symfony, every route at one point or another must return a response. In this example, we are not returning anything. This also goes to show that Symfony is very friendly while showing errors. It's telling us exactly what we need to do. So let's do it. Inside our method, return, new, Response, and inside the parentheses, just a string with HTML code.
Body, and inside body, Landon Hotel App. We are returning our response with some HTML attached to it. If we refresh, we'll see our HTML that we just created. While it is possible to embed HTML code inside our response, this goes against the whole concept of MVC. What we want to do is show a view, so let's do that. In our file structure, views are saved on APP, resources, views, which is where Symfony will look for the view files.
In this case, it is a good practice to have a folder for a controller, and a file for each method, unless we share a view. Let's update and refresh. Return, this, render, admin/index.html.twig. And if we refresh on the browser, we now see our application. While it is not mandatory to name our routes, it is a good practice to do so in order for us to identify them and to easily generate the URLs.
To name a route, we just add the following to the annotation. Comma, name, and we'll just name this home. This also serves as a good starting point to send parameters to our controllers. Let's go ahead and create the routes for the rest of the controllers, clients and reservations. I'm going to go to my ClientsController file and add the routes. We'll call this route index_clients.
In showIndex, I'm going to show the index of the clients and at some point, we'll show the list of clients that we have. The showDetails route will show a form to modify a single client. And we'll call this route modify_client. For new clients, we'll have a showNew method, so let's add the route to it. And we'll name this route new_client.
Let's go ahead and save and open up our ReservationController. And we'll just add the route to show the current reservations. And we'll name this route reservations.
- Installing Symfony
- Setting up the local environment
- Exploring the Symfony file structure
- Creating routes
- Sending parameters to a controller
- Using views
- Updating routes
- Creating templates with Twig
- Data modeling with Symfony
- Demo application deployment