How to build a simple app pulling contents from Pokemon API¶
Tutorial author: Simonarde Jr
In this example, we are going to build a simple app to show some info about a chosen Pokemon.
To get started, you'll need:
Creating a Simple App¶
First, we need to create a new project using Minicli template app, using your terminal of choice, enter the following command:
composer create-project --prefer-dist minicli/application pokemonApiConsumer
This will give us a starting point to build our app.
Creating a command¶
Minicli will register all commands inside the directory
The name you give to a folder inside
Command is the name of your Command Namespace, so, in this example, we will create a folder called
Pokemon. Cd to our app root folder and type the following command:
Now, we have to create our Controller file, this is the actual name of the command you want to execute, so, in this example, we will create a command called
Put the following code inside
We will implement
printPokemonInfo in a minute, but, right now, you can call your command to see if works using:
You should see something like:
Showing info about bulbasaur
In this example, we are using PokeAPI. To pull the info about our chosen pokemon, we are going to use the folowing endpoint
https://pokeapi.co/api/v2/pokemon/<name>, and, if you see the code we have putted inside
FetchInfoController.php, we have a method called
get, which uses curl to make a request to a given url. Let's use it then, inside
If you run fetchinfo command again, you will not see any new output, that's because we are making the request to PokeAPI, but not showing anywhere. Let's fix this!
Now we will display the info PokeAPI has returned in your last request. Open
FetchInfoController.php and replace
printPokemonTableInfo content with the following:
fetchinfocommand again, you will notice that the info will not be shown to us. That's because we have implemented the
printPokemonTableInfo, but we are not calling it. Let's call it then:
You should see some info now! Have fun!
* disclaimer: this is a very basic piece of code, and it is, by no means, the best and the cleanest way to implement a command using Minicli.