1 00:00:00,180 --> 00:00:04,250 We're going to use the Slim skeleton to set up our application. 2 00:00:04,250 --> 00:00:09,833 Let's run, composer create-project 3 00:00:09,833 --> 00:00:15,747 slim/slim-skeleton php_rest_api, 4 00:00:15,747 --> 00:00:19,530 as the name of our project. 5 00:00:23,328 --> 00:00:27,151 Now we can cd into php_rest_api. 6 00:00:30,428 --> 00:00:33,460 Now I can open these files in an editor. 7 00:00:33,460 --> 00:00:40,026 In our public folder, we have an .htaccess file and our index.php file. 8 00:00:40,026 --> 00:00:43,114 So let's try running our application. 9 00:00:43,114 --> 00:00:48,204 In MAMP, I'll go to Preferences > Web Server, 10 00:00:48,204 --> 00:00:55,546 and select Documents > php_rest_api, and our public directory. 11 00:00:57,297 --> 00:00:59,116 We can then start the servers. 12 00:01:00,985 --> 00:01:04,000 Great, our application is running. 13 00:01:04,000 --> 00:01:06,030 Let's start setting things up. 14 00:01:06,030 --> 00:01:07,850 Let's start in the settings file. 15 00:01:09,360 --> 00:01:11,720 We're going to be working in our src directory. 16 00:01:12,850 --> 00:01:18,137 In this settings file, I'm going to be adding API and database settings. 17 00:01:18,137 --> 00:01:21,153 These could be set using environment variables, 18 00:01:21,153 --> 00:01:23,676 so check the notes for more information. 19 00:01:23,676 --> 00:01:25,501 First, I'll add the API. 20 00:01:28,894 --> 00:01:34,096 Api is going to equal 21 00:01:34,096 --> 00:01:39,271 version, v1, 22 00:01:39,271 --> 00:01:46,062 and the base URL, This is going to be our localhost. 23 00:01:51,993 --> 00:01:59,402 Now I can add the SQLite database, db, 24 00:02:02,977 --> 00:02:08,118 And for a SQLite database, I need the dsn, 25 00:02:09,162 --> 00:02:14,534 Which will be sqlite, And the database. 26 00:02:16,398 --> 00:02:19,759 I'm gonna put this database in the current src directory. 27 00:02:20,787 --> 00:02:26,852 And it's named course_reviews.db. 28 00:02:28,680 --> 00:02:32,760 You can download the database that I've supplied that has some test data in it 29 00:02:32,760 --> 00:02:33,780 already. 30 00:02:33,780 --> 00:02:36,070 I'm gonna move mine to this src directory. 31 00:02:45,160 --> 00:02:49,630 As you can see, I have the course_reviews.db. 32 00:02:49,630 --> 00:02:54,916 Now I'm ready to add the API and database dependencies to my dependency container so 33 00:02:54,916 --> 00:02:59,094 that I can access these dependencies throughout my application. 34 00:03:04,586 --> 00:03:06,878 Let's give us some space, and this will be our API. 35 00:03:10,542 --> 00:03:16,083 We'll say $container, ['api']. 36 00:03:24,092 --> 00:03:29,798 I'm going to set $api = $c->get('settings'), 37 00:03:32,825 --> 00:03:35,416 And the api settings. 38 00:03:35,416 --> 00:03:37,800 For right now, I'm just going to return the api. 39 00:03:41,995 --> 00:03:46,250 Remember that we need to add the semicolon after each of these containers. 40 00:03:50,018 --> 00:03:54,321 Now I need a container for PDO to connect to my SQLite database. 41 00:03:57,378 --> 00:03:58,563 We'll call this db. 42 00:04:04,950 --> 00:04:10,602 And we'll set our $db = $c->get('settings'), 43 00:04:13,163 --> 00:04:14,460 And our database settings. 44 00:04:16,430 --> 00:04:18,130 Then we'll create a new PDO. 45 00:04:22,577 --> 00:04:26,524 And we'll use $db['dsn'], 46 00:04:29,069 --> 00:04:35,840 And then a colon, and then our $db['database']. 47 00:04:35,840 --> 00:04:41,870 I'm also going to add some PDO attributes. 48 00:04:41,870 --> 00:04:46,270 These are things like error mode and default modes. 49 00:04:46,270 --> 00:04:48,320 Check the notes for more information. 50 00:04:48,320 --> 00:04:51,245 Then we'll return the $pdo. 51 00:04:52,700 --> 00:04:57,930 The last thing that I need to set up is autoloading for our own classes. 52 00:04:57,930 --> 00:05:05,400 Open composer.json, if you scroll down, you'll see the autoload-dev. 53 00:05:05,400 --> 00:05:07,560 This is for tests. 54 00:05:07,560 --> 00:05:12,610 We'll copy this section, and we'll change it to autoload. 55 00:05:12,610 --> 00:05:16,834 And instead of Tests, we're going to say App, and 56 00:05:16,834 --> 00:05:20,650 our app lives in our src directory. 57 00:05:20,650 --> 00:05:25,290 Before these changes will take effect, we need to run composer update. 58 00:05:26,700 --> 00:05:32,380 From within the php_rest_api directory, we run, composer update. 59 00:05:34,508 --> 00:05:39,018 Now that we've added our settings, dependencies, and autoloader, 60 00:05:39,018 --> 00:05:41,820 we're ready to build our API. 61 00:05:41,820 --> 00:05:45,150 Since an API is all about sharing data, 62 00:05:45,150 --> 00:05:48,580 the first thing we need to do is access that data