REST, CRUD and Controllers5:59 with Jonathan Barrios
Now that we’ve set up our Laravel application and created factories that generate our test data, we’re ready to explore the differences between Controllers and Resource Controllers. See you there!
[MUSIC] 0:00 Welcome back. 0:04 Now that we've set up our Laravel application and 0:05 created factories that generate our test data we're ready to explore 0:08 the differences between controllers and resource controllers. 0:13 The main difference between these two controllers is that resource controllers 0:18 include all of the CRUD goodies pre-built for 0:23 us making this development task a snap in the context of a REST API. 0:26 The key word is resource, and we'll use CRUD operations on our 0:33 API resources, such as authors and books in order to read, 0:38 add, modify or delete our list of books and authors. 0:43 CRUD is short for Create, Read, Update, and Delete. 0:49 Which corresponds to HTML verbs such as GET, POST, 0:53 PUT/PATCH, and DELETE, which we can use to create a REST API. 0:58 For example, we can create an author, read an author, 1:05 update and delete an author, which also applies to books. 1:09 Each HTML verb such as GET, POST, PUT/PATCH, and 1:14 DELETE gets a designated URI, an action and a route name. 1:19 To learn more about CRUD in HTML verbs, check out the teachers notes below. 1:26 So what exactly is the difference between REST and CRUD? 1:32 REST uses HTTP verbs such as GET, POST, PUT/PATCH, 1:38 and DELETE to perform basic operations such as creating, 1:43 reading, updating, and deleting or CRUD for short. 1:48 While a REST client or user interacts with a REST server using HTTP verbs, 1:53 the server understands this and carries out the requested CRUD operation. 2:00 For example, like watching a Treehouse course, creating a blog post, 2:06 updating a typo, appending your wish list, or deleting an email. 2:11 The critical distinction to remember is that REST is an API architecture. 2:16 And CRUD is a set of operations that interact with our resources, 2:22 such as authors and books. 2:27 To illustrate how to create controllers with CRUD operations, we'll create 2:30 a regular controller by hand, which does not contain any CRUD boilerplate. 2:35 Then we'll create another controller, but this time we'll use Laravel's 2:41 time saving resource controller with all of our CRUD operations built in. 2:46 Let's get started. 2:51 First let's exit the Tinker shell by typing exit. 2:53 Next let's build a regular controller so 3:01 we can compare it to a resource controller with this artisan command. 3:04 Next, let's navigate to our controllers in the app/http/controllersdirectory and 3:16 open the RegularController.php file. 3:23 You'll notice that it's mostly empty. 3:28 That is to say, you would have to build each function yourself. 3:30 Fair enough, but the ResourceController builds each CRUD function for 3:34 us, which makes building APIs much easier. 3:40 Next, let's build a resource controller like this. 3:44 Keep in mind that we will remove the edit and create functions 3:59 later in the course since our API has no UI or user interface. 4:04 To learn more about REST, APIs and CRUD, check out the teacher's notes below. 4:09 Next, open the ResourceController and 4:15 let's see what the CRUD boilerplate is all about. 4:17 Let's start with C for create. 4:21 Notice that Request is already imported for us, at the top. 4:24 In this case, the c matches both crud and create, so 4:29 the function is pretty straight forward. 4:33 It's also worth noting that APIs without a UI will use the store function and 4:38 the comments offer us a clue. 4:43 The create function uses a form and store does not. 4:46 No need to worry if you don't understand this concept right away. 4:51 It will make more sense as you progress through the course. 4:54 Next, we have read and there are two main types of read. 4:58 Read all authors or books in the @index function or 5:03 read one specific author or book in the @show function. 5:08 Next we have update and delete, which are both straightforward as well. 5:14 Now that we've compared the two kinds of controllers, 5:21 let's delete the RegularController and 5:25 the ResourceController before we move on to the next section. 5:27 See you there. 5:31 Great job. 5:32 We created a controller and a resource controller to compare 5:34 the types of controllers, one that is fairly empty, and 5:39 the latter with all of the CRUD functionality we need to build our API. 5:43 Next we're going to build our author and 5:49 book resource controllers with all the basic CRUD boilerplate pre-built for us. 5:51 See you there. 5:57
You need to sign up for Treehouse in order to download course files.Sign up