[0:00] [MUSIC] [0:05] Hi, I'm Jonathan, a full stack web developer and [0:08] instructor here at Treehouse. [0:12] In this course, we're going to build a REST API with Laravel, [0:16] one of the most popular PHP frameworks today. [0:20] Even if you've never built an API, [0:22] the concept is very similar to web applications. [0:26] They both take in a request and return a response. [0:30] The critical difference is how they respond. [0:33] Web applications return an HTML format. [0:37] While REST APIs most commonly return a JSON format, and sometimes XML. [0:44] So what is REST, exactly? [0:47] REST is an acronym that stands for Representational State Transfer. [0:53] An architectural style first represented by Roy Fielding in 2000. [0:58] Check the teachers notes below to learn more about Roy Fielding and [1:02] the six guiding constraints for all REST APIs. [1:08] The most important constraint to remember is statelessness. [1:12] Requests from clients to the server [1:14] must contain all of the information necessary to understand the request. [1:19] And cannot store any state context on the server, only on the client. [1:26] It's also important to remember that [1:28] application state is not the same as resource state. [1:33] Resource state, in the context of REST, [1:36] is the state of some data represented on the server at any point in time. [1:42] Application state, on the other hand, is everything necessary to keep [1:47] your application running, which is stored in the server's memory, [1:52] and is faster than storing and retrieving information from a database. [1:57] For example, to identify incoming requests, [2:01] such as creating, modifying, or deleting a resource. [2:06] Before taking this course, you should be familiar with setting up a Laravel [2:11] project with composer and have a basic understanding of rest API's using postman. [2:18] If you need to brush up on Laravel, Composer, REST APIs, or [2:23] Postman, check out the teachers notes below. [2:27] As always, you can use the controls to speed up or [2:30] slow down this video, and the captions are moveable. [2:35] So feel free to move the closed captions to suit your needs. [2:39] Finally, try to develop a solid habit of [2:42] reading the Laravel documentation regularly. [2:46] And be sure to select the correct Laravel version as well. [2:50] Laravel has excellent documentation. [2:53] And if you're not sure what version of Laravel you're working with, [2:56] navigate to your project directory in the terminal and [3:00] type php artisan --version to find out. [3:06] Building a REST API is a valuable skill that any PHP [3:10] developer should have in their wheelhouse of development skills. [3:15] Front-end frameworks are becoming more popular every day, and [3:18] they will all need a backend server responding to their requests. [3:23] Mobile applications have become part of everyday life around the world. [3:27] And yet, they all need to communicate with a database or [3:31] some infrastructure, most commonly REST. [3:35] In this course, we'll build a REST API with Laravel [3:39] that allows you to submit your favorite books and authors. [3:43] The REST API we're building from scratch can return a list of books with authors, [3:50] and also allows you to submit, update, and delete books and authors. [3:56] This aggregating of information is a common use case for APIs, [4:00] as well as SaaS applications, short for software as a service, [4:05] such as TurboTax, or Spotify. [4:09] Let's get started. [4:11] To get started, you'll need to install Laravel with Composer, [4:14] then create a new Laravel project in your preferred working directory. [4:19] For demonstration purposes, [4:21] I'll use laravel_apps in the desktop directory, like this. [4:28] Open the terminal and [4:29] navigate to the desktop using the cd Desktop/laravel_apps/ command, [4:35] then use the following command to create a Laravel project using Composer like this. [4:42] While Composer installs Laravel, it's worth noting that any existing Laravel [4:47] project that uses dependencies, will need to run the Composer update command [4:52] to install any dependencies used by the existing application. [4:56] Since this is a new project, we don't need to use the Composer update command, but [5:01] you will eventually need to use it when working with teams, or [5:05] when you want to run an existing Laravel application using dependencies. [5:10] To learn more about Composer update, and [5:13] Laravel mix, check out the teacher's notes below. [5:17] Next, let's navigate to our new Laravel project using [5:22] cd Laravel_rest_api/ then use the following command [5:27] to open our new Laravel project in Visual Studio code. [5:32] Finally, let's review our Laravel app by using the following command in [5:37] the integrated terminal like this. [5:45] Great job. [5:46] We have our Laravel application up and running. [5:51] Before we continue, let's open a new terminal so [5:54] we can continue serving our Laravel application using the first terminal. [5:59] Next, create a database, user, and [6:02] password which will add to the .env file like this [6:11] Note that some Mac OS users may need to use [6:15] an additional database variable named DB_SOCKET. [6:20] Remember, if you need to review how to create a database, user, and [6:24] password, or you're getting errors such as db-errors or [6:28] max length errors, be sure to check the teacher's notes below. [6:33] To test our new Laravel application and the database credentials we entered, [6:38] simply type php artisan migrate in the terminal. [6:45] Great job, we successfully migrated our database. [6:49] This next step is optional, but i like to use the default welcome view for [6:54] testing so I can see what our API looks like in the browser as we add new routes. [7:01] Let's edit the welcome view located in the resources/views directory, [7:06] or download the project files and simply paste the welcome blade like this. [7:13] First, change the titles on line 7 and 84 to build [7:18] a Laravel REST API Next, let's add the following links. [7:28] Replace the # sign with the URL for your README if you have one for [7:33] this project on GitHub Next, we're going to add the author's link. [7:41] The author's link uses the built in env function to access the app URL. [7:47] Cool. [7:49] Next, we're going to add the books link like this. [7:54] The books link also uses the built in env function to access the app URL. [8:00] Finally, let's add a Chrome extension link like this. [8:06] The handy Chrome extension named json-formatter displays JSON [8:10] in a more human readable format. [8:14] Let's take a look at the updated welcome view. [8:19] Nice job, we have a landing page, or welcome view, [8:23] showing off our application with a README, our author, and [8:28] book routes, and a link to the JSON formatter chrome extension. [8:34] We're off to a great start. [8:36] We've installed Laravel using Composer, added our database credentials to the env [8:42] file, and tested our database connection using the artisan command, [8:47] php artisan migrate. [8:51] We even updated the welcome view to make a useful landing page. [8:55] In the next section, we're going to delete our default migrations and [9:00] use artisan to create our projects database models and migrations. [9:05] See you there.