PHP code example of snappdoctor / php-rest-response
1. Go to this page and download the library: Download snappdoctor/php-rest-response 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/ */
snappdoctor / php-rest-response example snippets
...
use Symfony\Component\HttpFoundation\Response;
use DrResponse\DrResponse;
...
public function index(): Response
{
$status_code = Response::HTTP_OK;
$result = DrResponse::SUCCESS_RESPONSE;
$data = ['users' => "List of users for example"];
$messages = [];
$developer_message = '';
$response = new DrResponse($status_code,$result,$data,$messages,$developer_message);
return $response->send();
}
...
use Symfony\Component\HttpFoundation\Response;
use DrResponse\DrResponse;
...
public function index(): Response
{
return (new SuccessResponse())->send();
}
...
use Symfony\Component\HttpFoundation\Response;
use DrResponse\DrResponse;
...
public function index(): Response
{
$status_code = Response::HTTP_NOT_FOUND;
$result = DrResponse::ERROR_RESPONSE;
$data = [];
$messages = [
'entity' => ['entity not found!'] // This structure is recommended
];
$developer_message = 'Dear Front-End developer! You may have a typo!';
$response = new DrResponse($status_code,$result,$data,$messages,$developer_message);
return $response->send();
}
// or simply use
...
use Symfony\Component\HttpFoundation\Response;
use DrResponse\NotFoundErrorResponse;
...
public function index(): Response
{
$messages = [
'entity' => ['entity not found!'] // This structure is recommended
];
$developer_message = 'Dear Front-End developer! You may have a typo!';
return (new NotFoundErrorResponse($messages, $developer_message))->send();
}
// define a new class like this
namespace App;
use DrResponse\DrResponse;
use Symfony\Component\HttpFoundation\Response;
class ForbiddenErrorResponse extends DrResponse
{
public function __construct()
{
$status_code = Response::HTTP_FORBIDDEN;
$result = DrResponse::ERROR_RESPONSE;
$data = [];
$messages = [
'access' => [
'Forbidden!!'
]
];
$developer_message = '';
parent::__construct($status_code, $result, $data, $messages, $developer_message);
}
}
...
use Symfony\Component\HttpFoundation\Response;
use DrResponse\NotFoundErrorResponse;
...
public function index(): Response
{
return (new ForbiddenErrorResponse())->send();
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.