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