GET Search and Results3:49 with Alena Holligan
We'll set up the first feature of our GitHub client: the ability to search repositories. We'll use the wrapper library to pass our search parameter to GitHub, then use the results to display a list in our application.
Github API Easy Access Library Basic usage
The head of the library is the Milo\Github\Api class. Within this class you access the API by GET, POST, HEAD, PUT ... HTTP methods.
$api = new Milo\Github\Api; # Get all available emoticon URLs example $response = $api->get('/emojis'); $emojis = $api->decode($response); # Render a Markdown document example $data = [ 'text' => 'Check out this commit e41d17e4a4a9e5368e018fcac44ff42155bc8738', 'mode' => 'gfm', 'context' => 'milo/github-api', ]; $response = $api->post('/markdown', $data); $html = $api->decode($response);
The project files for this application are all set up and 0:00 ready to accept data from the API. 0:03 They use Bootstrap for styling and Twig for HTML templates. 0:05 I included these packages in the composer.json file. 0:09 So when we ran the composer require from the previous video 0:13 those packages were also installed. 0:16 If you are unfamiliar with Bootstrap Twig Templates or 0:19 Composer, please check the notes associated with this video. 0:22 Let's preview the current state of the project in a browser. 0:27 We have a home page that introduces people to the project, 0:34 a Search page that isn't actually providing search capabilities yet, 0:38 and a Project Repos page that reads the composer.LOC file and 0:43 gives us a list of packages used in this project. 0:47 We'll be adding two features, the search capability and 0:51 the ability to watch or on watch packages. 0:55 Let's go back to Workspaces and start with the search feature. 1:00 Open search.php. 1:05 After our Twig object, let's create a new API object. 1:08 Next, we need to check for search string. 1:21 If (!empty($_GET['q'])) 1:26 If the search string is set we can use that in our API GET method. 1:37 Let's start by creating a response. 1:43 $api->get( and now we're going to '/search/repositories/. 1:48 The get method accepts two optional parameters, 2:02 additional parameters for the URL and additional headers. 2:05 So we can add the search string as a second parameter. 2:10 The second parameter is an array, so we add 'q'. 2:14 And set it equal to filter_input(INPUT_GIT, 2:22 'q', FILTER_SANITIZE_STRING). 2:29 Next we decode the response so that we can access the repositories. 2:40 The search template is set up to display those repositories, so 2:55 all we need to do is pass the repositories as an argument named items. 2:59 Let's move the arguments out of the render call and 3:05 then we can add the items to that array. 3:08 Now we can add $args['items'] 3:23 = $repositories->items. 3:29 Let's test our search page in the browser. 3:36
You need to sign up for Treehouse in order to download course files.Sign up