Package Data | |
---|---|
Maintainer Username: | MaxHill |
Maintainer Contact: | max@hilloco.se (Max Hill) |
Package Create Date: | 2016-06-01 |
Package Last Update: | 2016-09-11 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2025-02-09 15:01:22 |
Package Statistics | |
---|---|
Total Downloads: | 30 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 1 |
Total Watchers: | 3 |
Total Forks: | 0 |
Total Open Issues: | 0 |
All the api setup I dread creating for every project. Concised down to a single plugin.
// config/app.php
...
Maxhill\Api\ApiServiceProvider::class,
...
// app/Http/Kernel.php
...
'jwt.auth' => 'Tymon\JWTAuth\Middleware\GetUserFromToken',
'jwt.refresh' => 'Tymon\JWTAuth\Middleware\RefreshToken',
...
$ php artisan vendor:publish --provider="Maxhill\Api\ApiServiceProvider"
$ php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"
$ php artisan jwt:generate
// app/Http/Kernel.php
'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken',
Require the user to be logged in to access a route:
Route::post('whatever', [
'uses' =>'WhateverController@index',
'middleware' => 'jwt.auth'
]);
authenticate
routesAuthentication is already setup. Just post to /authenticate
with the email and password of the user you want to sign in.
You may change the /authenticate
-route to whatever you like in the config file config/api.php
###Extend ApiController.
Make sure your api-controllers extends
Maxhill\Api\Http\Controllers\ApiController;
and not the default
App\Http\Controller
###Use transformers for returning.
Example user model transformer;
<?php
# app/Transformers/UserTransformer.php
namespace App\Transformers;
use App\User;
use League\Fractal\TransformerAbstract;
class UserTransformer extends TransformerAbstract
{
/**
* Turn this item object into a generic array
*
* @param User $user
* @return array
*/
public function transform(User $user)
{
return [
'id' => (int)$user->id,
'name' => $user->name,
'email' => $user->email
];
}
}
Using the UserTransformer
:
# app/Http/Controllers/WhateverController.php
public function index()
{
$users = User::all();
return $this->respondWithCollection($users, new UserTransformer);
}
###ApiController - methods
user() // Returns false or user object parsed from auth token
#####Respond with data:
respondWithItem()
respondWithCollection()
respondWithArray()
#####Error-responses:
respondWithError()
errorForbidden()
errorInternalError()
errorNotFound()
errorUnauthorized()