Resource Controllers2:22 with Jonathan Barrios
Welcome back! In this section, we’ll cover the differences between controllers and resource controllers used for CRUD operations. While this course doesn’t go into a full explanation of CRUD functionality, it’s crucial to understand how both work and, more importantly, how and when to use them. Laravel assigns common "CRUD" routes to resource controllers with a single line of code. For example, if you want to create a controller that handles all HTTP requests for "courses" stored by your application. As I mentioned earlier, you can use the Artisan command: php artisan make:controller -r, to make a resource controller with CRUD built-in. The optional -r flag is the only difference between the two Artisan commands for controllers and resource controllers.
Welcome back, in this section, we'll cover the differences between controllers and 0:00 resource controllers used for CRUD operations. 0:05 While this course doesn't go into full explanation of CRUD functionality, 0:08 it's crucial to understand how both work, and more importantly, how and 0:13 when to use them. 0:18 Laravel assigns common CRUD routes to resource controllers with a single line 0:19 of code. 0:24 For example, creating a controller that handles all HTTP requests for 0:24 courses stored by your application. 0:29 As I mentioned earlier, 0:32 you can use an Artisan command to make a resource controller with CRUD built in. 0:34 The optional -r flag is the only difference between the two 0:39 Artisan commands for controllers and resource controllers. 0:43 Let's create our first resource controller, so we can take a look at 0:47 the differences between a controller that returns a route and 0:51 a resource controller, by using the main controller Artisan command like this. 0:55 Once you've created the resource controller, 1:08 navigate to the App\Http\Controllers directory, 1:12 and open the ExampleController.php file. 1:16 You'll notice that both controllers are very much alike, with the exception that 1:20 the resource controllers have all of the CRUD functionality pre-built for you. 1:24 With one command, you get resource controllers out of the box, 1:29 how cool is that? 1:33 We won't use resource controllers in this course, 1:34 but it's useful to know how Laravel makes CRUD operations 1:38 rather intuitive to implement using the optional -r flag. 1:41 Without the -r flag, it's just a controller, 1:45 not a resource controller with CRUD functionality. 1:48 Resource controllers make building CRUD functionality a snap, and 1:51 the Artisan CLI makes it even easier to implement. 1:56 We can also use the middleware method to attach middleware to controller actions. 1:59 To learn more about middleware, check the teacher's notes below. 2:05 In the next session, we are going to build all the views we need for our Laravel app. 2:09 Then we'll test our routes, closures, and controllers, 2:14 to see how they display the views in the browser, see you there. 2:17
You need to sign up for Treehouse in order to download course files.Sign up