Download the PHP package maxhill/api without Composer
On this page you can find all versions of the php package maxhill/api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package api
Laravel api setup
All the api setup I dread creating for every project. Concised down to a single plugin.
- It gives you JSON-webtoken authentication (using the awesome tymondesigns/jwt-auth).
- It gives you model transformation (using the fantastic fractal-package)
- It also gives you some sweet api-helper methods for returning resources and errors and more.
Install
- Add service provider.
// config/app.php ... Maxhill\Api\ApiServiceProvider::class, ...
- Add route middleware.
// app/Http/Kernel.php ... 'jwt.auth' => 'Tymon\JWTAuth\Middleware\GetUserFromToken', 'jwt.refresh' => 'Tymon\JWTAuth\Middleware\RefreshToken', ...
- Publish vendors.
$ php artisan vendor:publish --provider="Maxhill\Api\ApiServiceProvider" $ php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"
- Generate jwt-secret.
$ php artisan jwt:generate
- Remove csrf-token middleware.
// app/Http/Kernel.php 'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken',
Usage
Authentication
- Use route middleware
Require the user to be logged in to access a route:
Route::post('whatever', [
'uses' =>'WhateverController@index',
'middleware' => 'jwt.auth'
]);
- Use
authenticate
routes
Authentication 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
Controllers
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;
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()
- `errorWrongArgs()
All versions of api with dependencies
laravel/framework Version 5.2.*
league/fractal Version ^0.13.0
tymon/jwt-auth Version 0.5.*