PHP code example of nimaebrazi / laravel-api-presenter
1. Go to this page and download the library: Download nimaebrazi/laravel-api-presenter 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/ */
nimaebrazi / laravel-api-presenter example snippets
'providers' => [
...
\LaravelApiPresenter\ApiPresenterServiceProvider::class,
...
]
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use LaravelApiPresenter\Contract\ApiPresenterInterface;
use LaravelApiPresenter\Presenter\Model\ApiPresenterModel;
class UserController extends Controller
{
/**
* @var ApiPresenterInterface
*/
protected $apiPresenter;
public function __construct(ApiPresenterInterface $apiPresenter)
{
$this->apiPresenter = $apiPresenter;
}
public function find($id)
{
$user = User::find($id);
$apiPresenterModel = new ApiPresenterModel();
$apiPresenterModel
->withSuccessStatus()
->setMessage('Success fetch!')
->setMainKey('user')
->setData($user->toArray());
return $this->apiPresenter->present($apiPresenterModel);
}
}
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use LaravelApiPresenter\Contract\ApiPresenterInterface;
use LaravelApiPresenter\Presenter\Model\ApiPresenterModel;
class UserController extends Controller
{
/**
* @var ApiPresenterInterface
*/
protected $apiPresenter;
public function __construct(ApiPresenterInterface $apiPresenter)
{
$this->apiPresenter = $apiPresenter;
}
public function find()
{
$ids = [1,2,3];
$user = User::find($ids);
$apiPresenterModel = new ApiPresenterModel();
$apiPresenterModel
->withSuccessStatus()
->setMessage('Success fetch!')
->setMainKey('user')
->setData($user->toArray());
return $this->apiPresenter->present($apiPresenterModel);
}
}
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use LaravelApiPresenter\Contract\ApiPresenterInterface;
use LaravelApiPresenter\Presenter\Model\ApiPresenterModel;
class UserController extends Controller
{
/**
* @var ApiPresenterInterface
*/
protected $apiPresenter;
public function __construct(ApiPresenterInterface $apiPresenter)
{
$this->apiPresenter = $apiPresenter;
}
public function find($id)
{
$user = User::find($id);
$apiPresenterModel = new ApiPresenterModel();
$apiPresenterModel->withSuccessStatus()
->setMessage('Success fetch!')
->setMainKey('user')
->cacheable()
->setCacheKey("user_{$id}")
->setData($user->toArray());
return $this->apiPresenter->present($apiPresenterModel);
}
}
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use LaravelApiPresenter\Contract\ApiPresenterInterface;
use LaravelApiPresenter\Presenter\Model\ApiPresenterModel;
class UserController extends Controller
{
/**
* @var ApiPresenterInterface
*/
protected $apiPresenter;
public function __construct(ApiPresenterInterface $apiPresenter)
{
$this->apiPresenter = $apiPresenter;
}
public function all(Request $request)
{
$limit = $request->has('limit') ? $request->input('limit') : 5;
$users = User::paginate($limit);
$apiPresenterModel = new ApiPresenterModel();
$apiPresenterModel
->withSuccessStatus()
->setMessage('Success fetch!')
->setMainKey('users')
->withMeta()
->cacheable()
->setCacheKey("users")
->setData($users->toArray());
return $this->apiPresenter->present($apiPresenterModel);
}
}
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use LaravelApiPresenter\Contract\ApiPresenterInterface;
use LaravelApiPresenter\Presenter\Model\ApiPresenterModel;
use LaravelApiPresenter\Presenter\Model\MetaModel;
class UserController extends Controller
{
/**
* @var ApiPresenterInterface
*/
protected $apiPresenter;
public function __construct(ApiPresenterInterface $apiPresenter)
{
$this->apiPresenter = $apiPresenter;
}
public function all(Request $request)
{
$limit = $request->has('limit') ? $request->input('limit') : 5;
$users = User::paginate($limit);
$metaModel = new MetaModel();
$metaModel->setCurrentPage(1)
->setLastPage(3)
->setTo(3)
->setTotal(50);
$apiPresenterModel = new ApiPresenterModel();
$apiPresenterModel
->withSuccessStatus()
->setMessage('Success fetch!')
->setMainKey('users')
->withMeta()
->cacheable()
->setCacheKey("users__limit_1__page_1")
->setData($users->toArray());
return $this->apiPresenter->present($apiPresenterModel);
}
}
LaravelApiPresenter\Presenter\Model\MetaModel
...
$this->apiPresenter->response()
->header('X-Header-One', 'Header Value One ')
->header('X-Header-Two', 'Header Value Two');
...
text
laravel_cache:user_1
text
laravel_cache:users__limit_3__page_1