1. Go to this page and download the library: Download lujo/lumen-rest library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lujo / lumen-rest example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
protected $table = 'article';
protected $fillable = [
'title',
'description',
'content'
];
// Optional realtions and other Eloquent stuff
public function articleAuthor() {
return $this->hasOne('App\Models\Author');
}
}
namespace App\Http\Controllers;
use Illuminate\Database\Eloquent\Model;
use App\Models\Article;
use Lujo\Lumen\Rest\RestController;
class ArticleController extends RestController {
/**
* @return Model
*/
protected function getModel() {
return new Article();
}
// Optional override, transforms every model before returning it from API.
protected function beforeGet($model) {
return $model;
}
// Optional override, transform received request before creating new model from it.
protected function beforeCreate($request) {
return $request->all();
}
// Optional override, transform received request before updating existing model from it.
protected function beforeUpdate($request) {
return $request->all();
}
// Optional override, perform some action on/with model before it is deleted.
protected function beforeDelete($model) {
return null;
}
// Optional override, specify list of relations to return on specific action
protected function getWith($request, $action) {
if($action === 'INDEX') {
return ['text'];
}
return ['author', 'comments', 'text'];
}
// Optional override, specify list of where statements to return on specific action
protected function getWhere($request, $action) {
if($action === 'DELETE') {
return [['name', 'Test']];
}
return [['status', 'ACTIVE'], ['enabled', true]];
}
// Optional override, specify additional where query statement in form of a anonymous function
protected function getWhereFunction($request, $action) {
return function($q) {
$q->where('name', 'Test')->orWhere('status', 'ACTIVE');
};
}
// Optional override, when INDEX method is called and this method returns true, the returend JSON will contain
// count data used for pagination e.g. {result_count: 10, total_count: 45, data: [...results]}
protected function withCountMetadata($request) {
return false;
}
}
use Laravel\Lumen\Routing\Router;
use Lujo\Lumen\Rest\RestRoute;
/**
* @var $router Router
*/
// This will create all article routes ('INDEX', 'ONE', 'CREATE', 'UPDATE', 'DELETE') for routes /articles/*
RestRoute::route($router, 'articles', 'ArticleController', 'middleware1');
// This will create only 'INDEX' and 'ONE' for routes /users/*
RestRoute::route($router, 'users', 'UserController', ['middleware1', 'middleware2'], ['INDEX', 'ONE']);
// This will create only 'INDEX', 'CREATE' and 'UPDATE' routes for /example/* but apply middlewares only on CREATE and UPDATE
RestRoute::route($router, 'examples', 'ExampleController', ['CREATE' => ['middleware1', 'middleware2'], 'UPDATE' => 'middleware2'],
['INDEX', 'CREATE', 'UPDATE']);
// This will create all routes and apply middleware1 and middleware2 to INDEX and ONE route, on others will middleware3 be applied.
RestRoute::route($router, 'users', 'UserController', ['INDEX,ONE' => ['middleware1', 'middleware2'], 'middleware3']);
// Example subgroup of routes (/article/authors/*) with middleware
$router->group(['prefix' => 'article', 'middleware' => 'middleware1'], function ($subRoute) {
RestRoute::route($subRoute, 'authors', 'AuthorController');
}
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.