[0:00] Hey everyone, welcome to the first [0:02] episode of my Laravel API course for [0:04] beginners. In this video series, I'm [0:06] going to be covering all the basic [0:08] information you need to know to start [0:09] building your own APIs inside Lavel. So [0:13] in this course, I'm assuming you guys [0:14] have the basics of Ludal down and now [0:17] you're trying to learn how to build your [0:19] own APIs. Okay, so let's get right into [0:21] it. So first thing first, you don't [0:23] actually need to create a new project to [0:25] start adding APIs. So if you already [0:27] have an existing project, you can follow [0:29] along. But if you don't, the first step [0:31] is for us to create a new project or a [0:33] new Laravel project. So I already have [0:35] the command here. So I'm going to go [0:36] ahead and run Laravel new. And this is [0:38] my project's name. You can name yours [0:40] whatever you like. Hit enter. [0:43] Now we don't necessarily need any [0:45] starter kits to add APIs, but just to [0:48] make things a bit easier, I'm going to [0:49] go ahead and select lot of a breeze. So, [0:51] we have some basic boiler plate, you [0:53] know, for logging, log out, uh, things [0:55] of that nature. If you're not familiar [0:57] with starter kits, you can skip this [0:59] part or just follow along as I'm doing. [1:02] And next up, for this one, I'm just [1:03] going to go blade with Alpine, the first [1:05] option. It doesn't really matter which [1:07] one you select. So, I'm just going to go [1:08] ahead with the first option. I do [1:10] recommend you skip the API only because [1:12] I'm going to be covering that again. So, [1:14] we're just going to go with the first [1:15] option. And I don't care about dark mode [1:17] for the course. I'm going to select [1:19] pest. And that's it. So now we should [1:21] have a new Lo project. If you already [1:23] have an existing one, you can skip this [1:25] part and follow on the next step. [1:29] All right. For database, you can select [1:30] any of these you like. I'm going to go [1:32] with skill light because it doesn't [1:33] require any setup. And I'm also going to [1:35] run the default migrations. [1:39] All right. Now, with the setup done, [1:40] let's go ahead and open up uh this [1:42] project in our code editor real quick. [1:46] Okay guys, so my project installation is [1:48] done. And I have also gone ahead and [1:50] opened the project in my editor. So [1:53] before we actually get started, you [1:55] might be looking at if you are looking [1:56] at some older courses online, uh you [1:59] might end up noticing that there is no [2:01] API. Here some some older courses I [2:04] think prior to Larl 9 or 10. There used [2:07] to be a default API. PHP that is no [2:10] longer here. So the first step we need [2:11] to do is actually to install API support [2:14] for Laravel. And for that we need to [2:16] actually run a command. So, open up your [2:17] terminal and make sure you have [2:20] navigated to your project root [2:21] directory, which is where I am right [2:23] now. And we need to run the following [2:25] artisan command. So, I'm going to run [2:26] PHP artisan [2:30] install API. [2:33] And what this command will do, it will [2:35] add some boiler plate code that allows [2:37] us to support APIs. It also add some [2:40] additional changes that I'm going to [2:42] cover real quick. It's also going to ask [2:44] you to publish some new database [2:46] migrations. I'm going to go ahead and [2:48] hit yes. Do that as well. So once you do [2:51] that, the first thing you notice is it [2:53] added a new database migration file. So [2:56] it's under database migrations. It's [3:00] this file. So it's going to be create [3:03] personal access tokens. Uh we're going [3:05] to be covering that on later episode. [3:07] This is for a lot of sanctum and it's [3:09] going to allow us to do authentication [3:11] and authorization for our APIs. So it's [3:14] quite powerful. Uh one more change that [3:16] it's going to also do is it's going to [3:18] add this new API.php [3:20] route file. So this one is very useful. [3:22] We're going to actually be defining all [3:24] our APIs inside this file. Okay, let's [3:28] get right into it. So now that we have [3:29] done the initial setup, it's actually [3:31] very easy to create our first API. It's [3:34] very similar to how you would do it with [3:35] normal web.php. PHP. All you have to do [3:38] is just simply do route [3:40] followed by the HTTP request you want. [3:43] So in this case, I'm going to go with a [3:44] get request. [3:46] Next up, you need to define your URL or [3:49] URI. In this case, we're going to be [3:51] building something super simple. For the [3:52] first episode, I'm going to say hello [3:53] for hello world. And last but not least [3:56] here, you can either pass in a [3:59] controller or for now, I'm going to keep [4:01] it super simple and I'm going to have an [4:02] inline function, basically an anonymous [4:05] function. So I'm going to say function [4:07] and I'm just going to define net over [4:10] here. Okay, on the next episode we got [4:11] are going to have a controller. So don't [4:13] worry about that. Now that we have [4:15] defined our route, usually if you have [4:17] an API, you want to return some sort of [4:19] JSON in your response. That's actually [4:21] very easy to do with Laravel. All we [4:23] have to do is do return response. [4:27] And on this response helper, there is a [4:29] JSON over here and this will allow you [4:32] to return JSON to your clients super [4:34] easily. Okay, so for the first parameter [4:37] is going to be what we are returning, [4:39] right? So that's going to be the [4:40] response of our API and this is going to [4:42] take an associative array in PHP and you [4:46] can return anything you want. So let's [4:47] say I want to return a message [4:52] and I'm going to return let's say hello [4:56] world. All right, just for now some [4:59] basic content returned. Don't forget the [5:02] semicolon. [5:04] And that's it. Okay, this is our first [5:07] super basic API. So, let's go ahead and [5:09] save this and open this up in a browser. [5:13] So, I have my project open over here. [5:16] Uh, now one thing you may notice if you [5:18] go ahead and do /hello, [5:21] you will actually get this 404 not [5:24] found, right? Even though if you look at [5:26] our route, it is actually called hello. [5:29] So uh what lable does is if you are [5:31] defining an API inside this API.php file [5:34] is it will always add an API prefix [5:38] before all your routes. So it will [5:40] always be API and then followed by [5:42] hello. So that's what actually we need [5:44] to put in. So I'm going to go back and [5:46] change it up to API hello. [5:50] And now you guys can see we are getting [5:52] actually a JSON response from our API. [5:58] Now a question you might have is like [6:00] hey why do we need to put our routes [6:02] inside this API.php and why can't I just [6:05] for example go ahead and put it inside [6:06] this web.php right what's the use case [6:09] is it just the prefix there's actually [6:11] more than that so usually if you have an [6:14] API it's going to be a stateless request [6:17] right while when you have this like [6:19] web.php PHP files it's going to be [6:20] stateful right so for example the user [6:22] might already be logged in they have [6:24] some sessions they have some cookies but [6:26] when they are using APIs they're not [6:28] going to have any cookies they're not [6:29] going to have any existing sessions so [6:31] it's going to be stateless and larville [6:32] is going to handle these two routes [6:34] separately also usually well actually [6:38] not usually always by default Laravel is [6:40] going to do CSRF authentication on [6:43] web.php PHP routes while with API routes [6:47] because it is stateless there is no CSRF [6:50] protection right so if you were for [6:52] example to put some sort of post request [6:53] or put request inside this web.php PHP [6:56] and you try to access it or call it [6:58] similar to like a normal API, Laravel is [7:01] going to be doing that CSRF protection [7:03] check and all your calls are going to [7:06] fail. Okay, so it's very important for [7:07] us to put all our APIs under this [7:11] API.php file to kind of keep it super [7:13] simple. Okay, [7:15] and one more change, I'll just quickly [7:17] show it to you guys. If you go actually [7:19] under your uh under your Bootstrap [7:22] app.php PHP file. You will also notice [7:24] that now there is this new line over [7:28] here. So this is actually where Laravel [7:30] is defining it. Right? So all the APIs [7:32] are going to be read under this routes [7:35] API.php file. Now later on on a later [7:38] episode I'm going to show you guys how [7:39] you can actually change this API prefix. [7:41] This is customizable. So none of none of [7:43] these are hardcoded. We can customize [7:45] them, but those are going to be for the [7:47] next episode. So that's it guys for [7:49] today's episode. Super basic. I just [7:51] wanted to cover how to get the initial [7:53] setup done and create your first API. On [7:56] the next episode, we're going to talk [7:58] about how to load data from the database [8:01] and also a bit more complex routes, [8:03] maybe having like dynamic URLs and [8:05] things of that nature. See you guys on [8:07] the next episode. Have a great day. Bye.