-
-
Notifications
You must be signed in to change notification settings - Fork 18
Building a Basic API Tutorial
Craig Smith edited this page Dec 19, 2019
·
9 revisions
For this tutorial we will be making use of the following (you can substitute your own tools for the most)
- Docksal
- Postman for api testing
- Laravel 6.x
- Laravel Passport
- This Package (Duh)
- React Admin - for our visual site
Perhaps more as we build. this tutorial will be broken down into smallish steps that can be done pretty quickly.
- Outside of the scope of this tutorial (and can be skipped if you use a different dev environment) Install Docksal & Postman on your machine
- Setup your dev environment and install laravel:
composer create-project --prefer-dist laravel/laravel apitutorial
- Install Laravel Passport:
composer require laravel/passport
-- follow the install instructions @ https://laravel.com/docs/6.x/passport#installation
- setup password grant @ https://laravel.com/docs/6.x/passport#creating-a-password-grant-client
- Setup some user seeding,
- run
php artisan make:seeder UserTableSeeder
- edit
database/seeds/DatabaseSeeder.php
and add to the run method$this->call(UsersTableSeeder::class);
- run
composer dump-autoload
followed byphp artisan db:seed
- run
- Install Postman
- Setup our first api endpoint: to http://xxx/oauth/token as a post with a json body of
{
"grant_type": "password",
"client_id": "client-id",
"client_secret": "client-secret",
"username": "[email protected]",
"password": "my-password",
"scope": "*"
}
once you trigger the call you should get a response looking like:"
{
"token_type": "Bearer",
"expires_in": 31622400,
"access_token": "xxx",
"refresh_token": "xxx"
}
From this point you can add any additional packages you want to use for permissions / roles gates etc, the scope of which is a bit to much for this tutorial. I would suggest https://docs.spatie.be/laravel-permission/v3/introduction/