1. Go to this page and download the library: Download sobored/mezzio-rest-helpers 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/ */
sobored / mezzio-rest-helpers example snippets
declare(strict_types=1);
namespace App\Handler;
use Mezzio\Hal\HalResponseFactory;
use Mezzio\Hal\ResourceGenerator;
use App\Entity\UserEntity;
use App\TableGateway\UserTableGateway;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use SoBoRed\Mezzio\Rest\Exception\NoResourceFoundException;
use SoBoRed\Mezzio\Rest\RestDispatchTrait;
class GetUserHandler implements RequestHandlerInterface
{
/**
* @var UserTableGateway
*/
private $userTable;
/**
* Use REST Helper RestDispatchTrait to gain
* $this->resourceGenerator, $this->responseFactory,
* and $this->createResponse()
*/
use RestDispatchTrait;
public function __construct(
UserTableGateway $userTable,
ResourceGenerator $resourceGenerator,
HalResponseFactory $responseFactory
)
{
$this->userTable = $userTable;
$this->resourceGenerator = $resourceGenerator;
$this->responseFactory = $responseFactory;
}
public function handle(ServerRequestInterface $request) : ResponseInterface
{
$id = $request->getAttribute('id', false);
$user = $this->userTable->get((int)$id)->current();
if (! $user instanceof UserEntity) {
// Throw a REST Helper Exception for HAL-compliant problem details in response
throw NoResourceFoundException::create("User with id `{$id}` not found");
}
// Return a HAL-compliant response that contains the record requested
return $this->createResponse($request, $user);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.