1. Go to this page and download the library: Download sehrgut/laravel5-api 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/ */
sehrgut / laravel5-api example snippets
use SehrGut\Laravel5_Api\Controller as ApiController;
use App\Models\Post;
class PostsController extends ApiController
{
protected $model = Post::class;
}
protected $key_mapping = [
// Maps the `{post_id}` url parameter to the model's `primary_key` attribute/db column
'post_id' => 'primary_key'
];
use SehrGut\Laravel5_Api\Exceptions\Exception as SehrGutApiException;
class Handler extends ExceptionHandler
{
public function render($request, Exception $exception)
{
if ($exception instanceof SehrGutApiException) {
return $exception->errorResponse();
}
// Possibly other checks
return parent::render($request, $exception);
}
}
use SehrGut\Laravel5_Api\Controller as ApiController;
use App\Models\Post;
class PostsController extends ApiController
{
protected $model = Post::class;
protected $relations = [
'comments', // Side-load the 'comments' relation within posts
'comments.author' // Side-load the 'author' relation in nested comments
];
protected $counts = [
'comments', // Add 'comments_count' to posts
'comments.responses' // Add `responses_count` to nested comments
];
}
use SehrGut\Laravel5_Api\Validator;
class PostValidator extends Validator
{
protected static $rules = [
'title' => '
use SehrGut\Laravel5_Api\Transformers\Transformer;
class PostTransformer extends Transformer
{
/**
* Correct the date format of the member_since attribute
*/
formatMemberSince($value)
{
return $value->toDateString();
}
}
use SehrGut\Laravel5_Api\ModelMapping as BaseModelMapping;
use App\Models\Post;
use App\PublicApi\V1\Transformers\PostTransformer;
use App\PublicApi\V1\Validators\PostValidator;
class ModelMapping extends BaseModelMapping
{
protected $transformers = [
Post::class => PostTransformer::class
];
protected $validators = [
Post::class => PostValidator::class
];
}
use SehrGut\Laravel5_Api\Controller;
class BaseController extends Controller
{
protected $model_mapping_class = ModelMapping::class;
}
use SehrGut\Laravel5_Api\Plugins\Paginator;
use SehrGut\Laravel5_Api\Plugins\SearchFilter;
class PostsController extends BaseController
{
protected $plugins = [
Paginator::class,
SearchFilter::class,
];
}
use SehrGut\Laravel5_Api\Plugins\SearchFilter;
class PostsController extends BaseController
{
protected $plugins = [SearchFilter::class];
protected function afterConstruct()
{
$this->configurePlugin(SearchFilter::class, [
'searchable' => ['name', 'description'], // compare to those fields on the model
'search_param' => 'query', // `?query=some+search+query`
]);
}
}
namespace App\Api\V1\Plugins;
use SehrGut\Laravel5_Api\Plugins\Plugin;
use SehrGut\Laravel5_Api\Hooks\AdaptCollectionQuery;
use SehrGut\Laravel5_Api\Hooks\AdaptResourceQuery;
/**
* Just an example: `dd()` all queries instead of executing them.
*/
class DieAndDumpQuery extends Plugin implements AdaptCollectionQuery, AdaptResourceQuery
{
$default_config = [
'option' => 'Reasonable default',
];
protected function adaptCollectionQuery()
{
dd($this->context->query);
}
protected function adaptResourceQuery()
{
dd($this->context->query);
}
}