Introducing API Controller Flag0:00
Next up, we have a quickie. If we take a look at the documentation for constructing a controller, you'll now see a new flag or option here for API. So you're likely already familiar with creating a resource controller. We'll add the resource flag here, and you'll now see within VueController, you have a class populated with all seven of the standard resourceful options. However, when you're constructing an API, some of these are unnecessary. For example, if I'm creating a API resource, well, create is unnecessary. We don't need to show a page to create the resource, so that's unnecessary. And of course, the same will be true for edit. So let's give this a shot. I'm once again going to create a controller. This time it'll be an API resource. So we'll say API, and don't forget to double escape that, by the way. We'll call it PostController. And yeah, let's do this in a couple steps. So first,
Generating an API Resource Controller0:48
be an API resource. So we'll say API, and don't forget to double escape that, by the way. We'll call it PostController. And yeah, let's do this in a couple steps. So first, without any flag whatsoever, you get an empty class. But this time, let's run it again. We'll call it Post2, and we'll add the API flag. Now, once again, this is new in Laravel 5.6. You'll see we've now populated all of the actions, but of course, the create and edit actions have been removed, as they will be unnecessary. So that means when you're constructing your controllers, maybe CommentsController here, you can leave it blank for an empty class stub. You can add resource here to populate it with the seven common actions. Or, for your API-specific controllers, you can add the API flag here. And that'll give you the same as resource, simply minus those two unnecessary actions.
Choosing Resource vs API1:31
your API-specific controllers, you can add the --api flag here. And that'll give you the same as resource, simply minus those two unnecessary actions.
