1. Go to this page and download the library: Download rougin/dexter 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/ */
rougin / dexter example snippets
php
namespace Rougin\Torin\Depots;
use Rougin\Dexter\Depots\EloquentDepot;
use Rougin\Torin\Models\Client;
class ClientDepot extends EloquentDepot
{
protected $model;
public function __construct(Client $client)
{
$this->model = $client;
}
}
php
namespace Acme\Depots;
use Acme\Sample\UserUpdater;
use Rougin\Dexter\Depot;
class UserDepot extends Depot
{
// ...
/**
* Updates the specified item.
*
* @param integer $id
* @param array<string, mixed> $data
*
* @return boolean
*/
public function update($id, $data)
{
return UserUpdater::update($id, $data);
}
}
php
namespace Acme\Depots;
use Acme\Models\User;
use Illuminate\Database\Eloquent\Model;
use Rougin\Dexter\Depots\EloquentDepot;
class UserDepot extends EloquentDepot
{
/**
* @param \Acme\Models\User $user
*/
public function __construct(User $user)
{
$this->model = $user;
}
}
php
// index.php
use Acme\Depots\UserDepot;
use Acme\Models\User;
$depot = new UserDepot(new User);
// Create a new item ---------------
$data = array('name' => 'John Doe');
$data['email'] = '[email protected]';
/** @var \Acme\Models\User */
$item = $depot->create($data);
// ---------------------------------
// Find an item by its ID ---
/** @var \Acme\Models\User */
$item = $depot->find(1);
// --------------------------
// Paginate through items -------
/** @var \Rougin\Dexter\Result */
$result = $depot->get(1, 10);
/** @var array<string, mixed> */
$items = $result->toArray();
// ------------------------------
// Update an existing item ---------
$data = array('name' => 'Jane Doe');
$depot->update(1, $data);
// ---------------------------------
php
namespace Acme\Routes;
use Acme\Depots\UserDepot;
use Rougin\Dexter\Http\Response;
use Rougin\Dexter\Input;
use Rougin\Dexter\Route;
class Users extends Route
{
protected $user;
public function __construct(UserDepot $user)
{
$this->user = $user;
}
protected function setIndexData($params)
{
$query = new Input($params);
$limit = $query->asTrueInt('l');
$page = $query->asTrueInt('p');
$result = $this->user->get($page, $limit);
$result = $result->toArray();
return Response::toJson($result);
}
}
php
// index.php
use Acme\Depots\UserDepot;
use Acme\Routes\Users;
$depot = new UserDepot;
$route = new Users($depot);
/** @var \Psr\Http\Message\ResponseInterface */
$request = /** ... */
$response = $route->index($request);
php
namespace Acme\Routes;
use Rougin\Dexter\Route;
class Users extends Route
{
// ...
/**
* Checks if the items are allowed to be returned.
*
* @param array<string, mixed> $params
*
* @return boolean
*/
protected function isIndexValid($params)
{
return true;
}
}
php
namespace Acme\Routes;
use Rougin\Dexter\Http\Response;
use Rougin\Dexter\Route;
class Users extends Route
{
// ...
/**
* Returns a response if the validation failed.
*
* @return \Psr\Http\Message\ResponseInterface
*/
protected function invalidIndex()
{
return new Response(422);
}
}
php
namespace Acme\Routes;
use Rougin\Dexter\Http\Response;
use Rougin\Dexter\Route;
class Users extends Route
{
// ...
/**
* Executes the logic for returning an array of items.
*
* @param array<string, mixed> $params
*
* @return \Psr\Http\Message\ResponseInterface
*/
protected function setIndexData($params)
{
$result = $this->user->get($params['page'], $params['limit']);
return Response::toJson($result->toArray());
}
}
php
namespace Acme\Routes;
use Rougin\Dexter\Http\Response;
use Rougin\Dexter\Route;
class Users extends Route
{
// ...
/**
* Returns a response if the validation failed.
*
* @return \Psr\Http\Message\ResponseInterface
*/
protected function invalidDelete()
{
return new Response(404);
}
/**
* Checks if the specified item can be deleted.
*
* @param integer $id
*
* @return boolean
*/
protected function isRowValid($id)
{
return true;
}
/**
* Executes the logic for deleting the specified item.
*
* @param integer $id
*
* @return \Psr\Http\Message\ResponseInterface
*/
protected function setDeleteData($id)
{
$this->user->delete($id);
return Response::toJson('Deleted!', 204);
}
}
php
namespace Acme\Routes;
use Rougin\Dexter\Http\Response;
use Rougin\Dexter\Route;
class Users extends Route
{
// ...
/**
* Returns a response if the validation failed.
*
* @return \Psr\Http\Message\ResponseInterface
*/
protected function invalidStore()
{
return new Response(422);
}
/**
* Checks if it is allowed to create a new item.
*
* @param array<string, mixed> $parsed
*
* @return boolean
*/
protected function isStoreValid($parsed)
{
return true;
}
/**
* Executes the logic for creating a new item.
*
* @param array<string, mixed> $parsed
*
* @return \Psr\Http\Message\ResponseInterface
*/
protected function setStoreData($parsed)
{
$this->user->create($parsed);
return Response::toJson('Created!', 201);
}
}