2017 - 0111 - Advanced API in Laravel PDF
2017 - 0111 - Advanced API in Laravel PDF
URL: coderblock.com
Video Promo: bit.ly/coderblock-video-promo
About Me
About me
Questions:
Solution:
Build an API REST!
(Application Programming Interface)
+
Webservices
Advanced API in Laravel
- /getAgencies
- /getUsers
- /removeCustomer
- /updateAgenciesInfo
Advanced API in Laravel
API Routes
(the REST way!)
CRUD Request
C - Create (POST)
R - Read (GET)
U - Update (PATCH/PUT)
D - Destroy (Delete)
Advanced API in Laravel
API Routes
(the REST way!)
1. Entity Migrations
2. Seed Data
3. Models
4. RESTful Routes
5. Controllers
6. Service/Repository Pattern
7. Format your output with Transformers!
Advanced API in Laravel
Entity Migration
Seed Data
$service = app(UserService::class);
// use the factory to create a Faker\Generator
instance
$faker = Faker::create();
foreach(range(1, env('users')) as $index) {
$user = User::create([
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'email' => $faker->email,
'password' => bcrypt('member'),
]);
$service->create($user, 'member', 'member',
false);
}
Advanced API in Laravel
Controllers: Destroy
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$result = $this->service->destroy($id);
if ($result) {
return response()->json(['success' => 'user was deleted'], 200);
}
return response()->json(['error' => 'Unable to delete user'], 500);
}
About Me
SOLUTION
Transformers for the WIN!
Transformers
Pros:
- Easy to handle
- Separation of Concerns
- Transform data by resource for API output
- Item and Collection
- Easy embed related resources
About Me
Transformers
AgencyTransformer
/app/Transformer/AgencyTransformer.php
/**
* Turn this item object into a generic array
*
* @param Agency $item
* @return array
*/
public function transform(Agency $item)
{
return [
"id" => (integer) $item->id,
"name" => (string) $item->name,
"piva" => (string) $item->piva,
"maxPartecipants" => (integer) $item->max_partecipants,
"actualPartecipants" => (integer) $item->actual_partecipants,
"activation_fee" => (float) $item->activation_fee,
"monthly_fee" => (float) $item->monthly_fee,
"start_date" => (string) $item->start_date,
"end_date" => (string) $item->end_date,
"status" => (string) ($item->status != null) ? $item->status->label : null,
"user_id" => (string) ($item->users != null) ? $item->users->id : null
];
}
About Me
ApiController
It’s a best practices to use a Base ApiController to handle request response
class AgenciesController extends ApiController
What about..?
- Testing
- Pagination with Cursor
- Validation
- Authentication
- Better error handling
- OOP + Design Patterns
See ya on part 2! ;)
Our Platform
)
Coderblock
;
n!
lia
Ita
Coderblock è un Recruitment Community Marketplace che si pone l’obiettivo di divenire punto di collegamento fra aziende
to
MARKETPLACE WORKSPACE
PLATFORM
SOCIAL TEAM
Danilo Costa, CEO & Founder @ TeamNovatek
Grazie
per l’attenzione!