Clean Routes4:42 with Jonathan Barrios
You created a laravel app, connected the MySQL database, migrated then seeded the database using Factories, you built CRUD operations using controllers, removed database-specific data using Resources, and combined it all with building your very own REST API using the Laravel framework. In the next video, we're going to add the finishing touches to optimize our Routes and make them scalable and clean. Clean routes will allow you to work with large applications that use a large number of Routes. Let's get started!
[MUSIC] 0:00 Congratulations, and great job making it to the last section of this course. 0:05 You worked hard to get to this point and 0:11 used everything you learned to build a REST API from the very beginning. 0:13 You created a Laravel app, connected the MySQL database, migrated, 0:19 then seeded the database using factories. 0:24 You then built CRUD operations using controllers, 0:27 removed database specific data using resources. 0:30 And finally, 0:34 combined it all with building your very own REST API using the Laravel framework. 0:35 In the next video, we're going to add the finishing touches to optimize our routes 0:41 and make them scalable and clean. 0:46 Clean routes will allow you to work with larger applications that 0:49 use a large number of routes. 0:53 Let's get started. 0:55 Before we move on, we should delete the secret, like this. 0:58 Next, let's open the api.php file and take a look at our routes, 1:06 and use the route:list command to list them. 1:11 Imagine we're working with a massive application. 1:19 This list of routes would become difficult to read, and 1:22 the task of managing routes would get messy pretty quick. 1:26 To solve this problem, we'll use groups and prefixes for the authors, like this. 1:30 This is great, but we can refactor this even further, like this. 1:49 Let's use the route:list command and take a look at our routes. 2:10 We have all of the routes we would expect with one line of code. 2:15 But since we're building an API, we don't need edit or create. 2:19 So let's remove those, like this. 2:24 Sure enough, the edit and create routes are gone, so 2:41 there's no way to access them, perfect. 2:45 Let's do the same for the books routes, like this. 2:48 Let's take another look at our routes using the artisan route:list command. 3:06 Great job. 3:20 Our routes are nicely refactored, and 3:22 our API is more like a real API, without the edit and create routes. 3:24 But we can refactor this even more, like this. 3:29 Once again, we can verify that our routes are the same using the php 3:37 artisan route:list command, like this 3:42 Excellent job, you refactored your routes from eight lines of code 3:56 into four lines of clean-looking code. 4:01 You may have noticed that we've learned multiple ways of doing things. 4:04 And each time we found that Laravel offers an elegant solution 4:08 that usually saves us time and looks great. 4:12 We're almost done building our Laravel REST API. 4:15 But before we can finish, let's create better validation errors for 4:19 our API consumers. 4:23 You may ask yourself, what's wrong with the current errors? 4:26 Great question. 4:30 And in the last section of this course, 4:31 we'll answer questions like, how are validation errors generated exactly? 4:33 And how can I make them better? 4:38 See you there. 4:41
You need to sign up for Treehouse in order to download course files.Sign up