• Stars
    star
    404
  • Rank 103,206 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 8 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An API-friendly fork of Laravel. Authentication, error handling, resource filtering, sorting, pagination and much more included

Larapi

Larapi is an API-friendly fork of Laravel, batteries included. If you want to read a more in-depth description of the offerings and how to do modern API development in Laravel I have written a series of blogposts on the topic.

A Modern REST API in Laravel 5

Larapi comes included with...

  • Laravel 5.4
  • Laravel Passport for OAuth Authentication, including a proxy for password and refresh-token grants
  • A new directory structure optimized for separating infrastructure and domain code. Groups your controllers, models, etc. by resource-type. Read more and view the source code here
  • Optimus\Heimdal: A modern exception handler for APIs with Sentry and Bugsnag integration out-of-the-box
  • Optimus\Bruno: A base controller class that gives sorting, filtering, eager loading and pagination for your endpoints
  • Optimus\Genie: A base repository class for requesting entities from your database. Includes integration with Bruno.
  • Optimus\Architect: A library for creating advanced structures of related entities
  • Optimus\ApiConsumer: A small class for making internal API requests

Motivation

We maintain a fairly large Laravel API at Traede. Our philosophy is to utilize the framework as much as possible. However, we also strongly feel that the amount of people that use Laravel for API development is not as large as it could be. We feel that Laravel as a framework tries to focus more on traditional web development (i.e. rendering HTML). We try to modify the framework just a little bit so it is more suited for API development. Larapi is the result of the changes we have made at Traede in order to make Laravel more enjoyable as an API framework.

Installation

First clone the repository

git clone https://github.com/esbenp/larapi my-api

Install dependencies

composer install

Copy the .env file an create an application key

cp .env.example .env && php artisan key:generate

Migrate the tables.

php artisan migrate

Larapi comes with Passport include as the default authenticatin method. You should now install it using this command.

php artisan passport:install

Copy-paste the generated secrets and IDs into your .env file like so.

PERSONAL_CLIENT_ID=1
PERSONAL_CLIENT_SECRET=mR7k7ITv4f7DJqkwtfEOythkUAsy4GJ622hPkxe6
PASSWORD_CLIENT_ID=2
PASSWORD_CLIENT_SECRET=FJWQRS3PQj6atM6fz5f6AtDboo59toGplcuUYrKL

If you want to save it elsewhere or change the naming be sure to modify the LoginProxy in infrastructure/Auth/LoginProxy.php

Test installation

You can quickly test if the authentication works by creating an user using the include command.

php artisan users:add Esben [email protected] 1234

Now serve your application and try to request a token using cURL

php artisan serve
curl -X POST http://localhost:8000/login -H 'Content-Type:application/json' -d '
{
    "email":"[email protected]",
    "password":"1234"
}'

This should return a token.

{"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImM0MWZiOWFjZjkyZmRiY2RhYjE0ZmEwYTFlMzMwYjBjYTEwMmRiMTA1ZGI4MmZjYzllZGUwMjRiNzI2MjA2YjRhZDU4MGZhMjUxODU2Y2RkIn0.eyJhdWQiOiIyIiwianRpIjoiYzQxZmI5YWNmOTJmZGJjZGFiMTRmYTBhMWUzMzBiMGNhMTAyZGIxMDVkYjgyZmNjOWVkZTAyNGI3MjYyMDZiNGFkNTgwZmEyNTE4NTZjZGQiLCJpYXQiOjE0ODk5NTM3MDYsIm5iZiI6MTQ4OTk1MzcwNiwiZXhwIjoxNDg5OTU0MzA2LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.SmsEyCEXBiwSgl0yMcjvCxoZ2a_7D6GDJTxTs_J-6yzUeJkOofrSV7RRafO3VvUckrNqy5sGgglrwGH_HN7_lNPU6XcgaaNzbzf-g7vCSzCicJiYZVzxqJpZVwqQ4WIQrc0lYdk7suZ7hwQulOD_Z79JhBNh1KSAyo3ABWHiRjh9NR_-iAjvlCohh7nAETDeVqoMrR99m3fwQYOjdtvRBHJ8Ei-Kx3Gn1DyOXyh8eGa5-yDtj-ZVI9x66YMXlm8wk4IMA_Oh7KJISfdpoQs4fPyrGsFAxQMFp02qEW2fzKl2eesZeiIAyDGWE4StHsuY3E4jZL0P-pjv08j5W4CBP0P64gkNw_GdbxlPPA-qZUzJlc3EtjrzZ9WZq3JAKKCGy5I1jHECDOqaQ1z7axm6rmxRWmXmRGwwkne8QxfPlXsN0sm5q98mJckeqCLUuir1VPyFn5Z-B7D80-sc7Zm-7zi-awJtZUGMcHSo_yNHXjVGcbJwFk04xoIL2QzMXpOVPLaUdlBp_obCJhdzT5Bx0o5SDdK2LwgEwbMkksqmrTJ7ypoezsc3ihVQIrMelK2lNfkH_cDcVdD3ub8oFTthbA62U6atXaIADcsgTCgOtgQ2uXTIko_btjECgL35LZDd8UxiyQT3w-pDrELGDPx17DQCsIZDJ8mC1s6E0d7EPsA","expires_in":600}

Now try to request all users GET /users using the newly issued token.

curl http://localhost:8000/users -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImM0MWZiOWFjZjkyZmRiY2RhYjE0ZmEwYTFlMzMwYjBjYTEwMmRiMTA1ZGI4MmZjYzllZGUwMjRiNzI2MjA2YjRhZDU4MGZhMjUxODU2Y2RkIn0.eyJhdWQiOiIyIiwianRpIjoiYzQxZmI5YWNmOTJmZGJjZGFiMTRmYTBhMWUzMzBiMGNhMTAyZGIxMDVkYjgyZmNjOWVkZTAyNGI3MjYyMDZiNGFkNTgwZmEyNTE4NTZjZGQiLCJpYXQiOjE0ODk5NTM3MDYsIm5iZiI6MTQ4OTk1MzcwNiwiZXhwIjoxNDg5OTU0MzA2LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.SmsEyCEXBiwSgl0yMcjvCxoZ2a_7D6GDJTxTs_J-6yzUeJkOofrSV7RRafO3VvUckrNqy5sGgglrwGH_HN7_lNPU6XcgaaNzbzf-g7vCSzCicJiYZVzxqJpZVwqQ4WIQrc0lYdk7suZ7hwQulOD_Z79JhBNh1KSAyo3ABWHiRjh9NR_-iAjvlCohh7nAETDeVqoMrR99m3fwQYOjdtvRBHJ8Ei-Kx3Gn1DyOXyh8eGa5-yDtj-ZVI9x66YMXlm8wk4IMA_Oh7KJISfdpoQs4fPyrGsFAxQMFp02qEW2fzKl2eesZeiIAyDGWE4StHsuY3E4jZL0P-pjv08j5W4CBP0P64gkNw_GdbxlPPA-qZUzJlc3EtjrzZ9WZq3JAKKCGy5I1jHECDOqaQ1z7axm6rmxRWmXmRGwwkne8QxfPlXsN0sm5q98mJckeqCLUuir1VPyFn5Z-B7D80-sc7Zm-7zi-awJtZUGMcHSo_yNHXjVGcbJwFk04xoIL2QzMXpOVPLaUdlBp_obCJhdzT5Bx0o5SDdK2LwgEwbMkksqmrTJ7ypoezsc3ihVQIrMelK2lNfkH_cDcVdD3ub8oFTthbA62U6atXaIADcsgTCgOtgQ2uXTIko_btjECgL35LZDd8UxiyQT3w-pDrELGDPx17DQCsIZDJ8mC1s6E0d7EPsA'

This should return a response like so

{"users":[{"id":1,"name":"Esben","email":"[email protected]","created_at":"2017-03-19 19:59:15","updated_at":"2017-03-19 19:59:15"}]}

You can refresh a new token by requesting POST /login/refresh and logout using POST /logout

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.

More Repositories

1

pdf-bot

πŸ€– A Node queue API for generating PDFs using headless Chrome. Comes with a CLI, S3 storage and webhooks for notifying subscribers about generated PDFs
JavaScript
2,595
star
2

react-native-clean-form

Easy react-native forms using bootstrap-like syntax with redux-form+immutablejs integration. Styled using styled-components
JavaScript
479
star
3

bruno

A base API controller for Laravel that gives sorting, filtering, eager loading and pagination for your resources
PHP
164
star
4

heimdal

An Laravel Exception handler build specifically for APIs.
PHP
124
star
5

onion

A before/after middleware implementation
PHP
72
star
6

lumen-api-oauth

The code for a blog post I wrote about creating web apps using a Lumen API that is authenticated by OAuth2
PHP
61
star
7

oauth2-server-lumen

A lumen bridge for lucadegasperi/oauth2-server-laravel
PHP
56
star
8

jquery-oauth

A $.ajax wrapper for OAuth 2 access and refresh token management for use in a SPA
JavaScript
52
star
9

laravel-api-consumer

PHP
39
star
10

architect

Dynamically create API resource structures for related resources
PHP
39
star
11

redux-refresh-token

An extension to your Redux API middleware that will refresh access tokens when hitting a 401 response. Works with middlewares that use FSA-style actions.
JavaScript
37
star
12

distributed-laravel

PHP
30
star
13

genie

A base repository class for Eloquent to abstract away persistence layer from your business code
PHP
27
star
14

react-native-redux-form-example

Code for http://esbenp.github.io/2017/01/06/react-native-redux-form-immutable-styled-components/
JavaScript
22
star
15

wp-darkshell

A Next-Gen Wordpress Hacker Backend
PHP
15
star
16

fineuploader-server

PHP
7
star
17

pdf-bot-laravel

πŸ€– Laravel integration for pdf-bot. A Node microservice for generating PDFs using headless Chrome
PHP
5
star
18

larapi-series-part-2

PHP
4
star
19

cloudinary-url-resolver

Convert Cloudinary transformations and public ids to Cloudinary resource urls
JavaScript
4
star
20

laravel-batch-request

Batch consume own api
PHP
3
star
21

esbenp.github.io

Blog site
JavaScript
3
star
22

larapi-part-3

PHP
3
star
23

fineuploader-server-thumbnail-creator

PHP
3
star
24

redux-refresh-token-example

Example app for redux-refresh-token
JavaScript
3
star
25

fineuploader-server-cloudinary

PHP
2
star
26

fineuploader-client-knockout

JavaScript
1
star
27

laravel-package-boilerplate

PHP
1
star
28

knockout-null-observable

JavaScript
1
star
29

sentinel-lumen

PHP
1
star
30

react-native-swift-test

Objective-C
1
star
31

knockout-bootstrap-modal

JavaScript
1
star
32

knockout-subscriptions-manager

JavaScript
1
star
33

knockout-bulk-table

JavaScript
1
star
34

fineuploader-client

JavaScript
1
star
35

react-init

Boilerplate: Flow, Immutable, Redux, Redux Immutable, React Router, Styled Components, Webpack 3. Based on an ejected create-react-app.
JavaScript
1
star