Download the PHP package lujo/laravel-rest without Composer
On this page you can find all versions of the php package lujo/laravel-rest. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-rest
Laravel REST
Laravel package for creating simple REST APIs.
Installation
Install the package using composer by executing following command:
Or you can add in composer.json
the followign line:
And then run:
Description
This Laravel package consists of two classes: RestRoute
and RestController
.
RestRoute
does all the routing operations in functionroute(...)
for specific resource.RestController
should be extended by some other controller class for specific resource.
Usage
1. Create Eloquent model
In order to use this REST package, first you must create eloquent model for some resource.
Example Article model:
2. Create Controller
After model is created, you can create simple Controller class and EXTEND the RestController class from this package.
Also, you must implement function getModel()
which must return Eloquent model created in last step which is the
resource you want to expose through this REST API.
Example Controller class:
All optional override functions are not required if you are not using them, they can be safely left out from your controller class implementation.
3. Create routing
After previouse two steps are finished, open your Laravel routes in routes/web.php
, and create routing structure
using RestRoute
class static function route($router, $prefix, $controller, $include = null)
.
Example routes/web.php
rotuing:
Generated REST resource routes are in the following format:
Method | Path | Function | Description |
---|---|---|---|
GET | /resource | Index | Get all resources* |
GET | /resource/{id} | One | Get one resource |
POST | /resource | Create | Create new resource |
PUT | /resource/{id} | Update | Update resource |
DELETE | /resource/{id} | Delete | Delete resource |
* Get all resources has following HTTP GET URL parameters:
- skip - How many resources to skip (e.g. 30)
- limit - How many resources to retreive (e.g. 15)
- sort - Filed on which to sort returned resources (e.g. 'first_name')
- order - Ordering of returend resources ('asc' or 'desc')
Example HTTP GET request: http://site.com/api/resource?skip=30&limit=6&sort=resource_name&order=desc
Response:
Headers:
Body when method withCountMetadata() returns false:
Body when method withCountMetadata() returns true:
LICENSE
MIT