PHPackages                             maxhill/api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. maxhill/api

ActiveLibrary[API Development](/categories/api)

maxhill/api
===========

Laravel Api package

1.0.7(9y ago)130MITPHPPHP &gt;=5.5.9

Since Jun 1Pushed 9y ago1 watchersCompare

[ Source](https://github.com/MaxHill/larapie)[ Packagist](https://packagist.org/packages/maxhill/api)[ RSS](/packages/maxhill-api/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (2)Dependencies (3)Versions (9)Used By (0)

Laravel api setup
=================

[](#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](https://github.com/tymondesigns/jwt-auth)).
- It gives you **model transformation** (using the fantastic [fractal-package](http://fractal.thephpleague.com/))
- It also gives you some sweet **api-helper methods** for returning resources and errors and more.

Install
=======

[](#install)

1. Add service provider.

```
// config/app.php
...
Maxhill\Api\ApiServiceProvider::class,
...
```

2. Add route middleware.

```
// app/Http/Kernel.php
...
'jwt.auth' => 'Tymon\JWTAuth\Middleware\GetUserFromToken',
'jwt.refresh' => 'Tymon\JWTAuth\Middleware\RefreshToken',
...
```

3. Publish vendors.

```
$ php artisan vendor:publish --provider="Maxhill\Api\ApiServiceProvider"
$ php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"
```

4. Generate jwt-secret.

```
$ php artisan jwt:generate
```

5. **Remove** csrf-token middleware.

```
// app/Http/Kernel.php
'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken',
```

Usage
=====

[](#usage)

Authentication
--------------

[](#authentication)

- Use route middleware

Require the user to be logged in to access a route:

```
Route::post('whatever', [
    'uses' =>'WhateverController@index',
    'middleware' => 'jwt.auth'
]);
```

[Read more](https://github.com/tymondesigns/jwt-auth/wiki/Authentication)

- 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
-----------

[](#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;

```
