PHP code example of phpslides / status

1. Go to this page and download the library: Download phpslides/status 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/ */

    

phpslides / status example snippets




namespace App\Controller\Api;

use PhpSlides\Status;
use PhpSlides\Http\Interface\ApiController;

final class UserController implements ApiController
{
   public function index() {
      $status = new Status();
   }
}


use PhpSlides\Enums\ResponseType;

ResponseType::JSON;
ResponseType::HTML;
ResponseType::CSV;
ResponseType::XML;

new Status(ResponseType::JSON);



namespace App\Controller\Api;

use PhpSlides\Status;
use PhpSlides\Enums\ResponseType;
use PhpSlides\Http\Interface\ApiController;

final class UserController implements ApiController {
   public function index() {
      $status = new Status(Response::JSON);
      return $status->success();
   }
}


use PhpSlides\StatusCode;

$user = [
    "name": "John Doe",
    "email": "[email protected]"
];
return (new Status())->success($user, StatusCode::OK);


return (new Status(ResponseType::JSON))->error('User not Found', StatusCode::NOT_FOUND);



namespace App\Controller\Api;

use PhpSlides\Http\Interface\ApiController;
use PhpSlides\Http\ResponseType;
use PhpSlides\StatusCode;
use PhpSlides\Status;

final class UserController extends Controller
{
   public function index(int $user_id) {
      $status = new Status();

      if ($user_id === 1) {
         $user = [
             'name': 'John Doe',
             'email': '[email protected]',
             'user_id': $user_id
         ];

         $response = $status->success($user); // by default the second parameter is `StatusCode::OK`
      } else {
         // not found message
         $response = $status->error("User user_id=$user_id is not found", StatusCode::NOT_FOUND);
      }

      return $response; // return message as a JSON format `Response::JSON`
   }
}


$status = new Status(); // Defaults to JSON
$status = new Status(ResponseType::HTML); // Initializes with HTML response type

$httpStatus = $status->getStatus(); // Returns the current HTTP status code

$statusText = $status->getStatusText(); // Returns status text, e.g., "OK" for 200

$message = $status->getMessage(); // Returns the message set in the response

$response = $status->get(); // Returns the raw response data

$jsonResponse = $status->getJson(); // Returns the response as a JSON string

$status->set(['key' => 'value'], StatusCode::OK, StatusText::OK); // Sets custom response data

$status->setStatus(StatusCode::OK); // Manually set the status code

$status->setStatusText(StatusText::OK); // Manually set the status text

$status->setMessage('Success message'); // Manually set the response message

$errorResponse = $status->error('An error occurred', StatusCode::BAD_REQUEST); // Returns error response in JSON

$successResponse = $status->success(['message' => 'Operation successful']); // Returns success response in JSON

$response = Response::json(['key' => 'value'], StatusCode::CREATED); // Outputs JSON response

$response = Response::html(['key' => 'value'], StatusCode::OK); // Outputs HTML response

$response = Response::csv(['key' => 'value'], StatusCode::OK); // Outputs CSV response

$response = Response::xml(['key' => 'value'], StatusCode::OK); // Outputs XML response

$response = Response::array(['key' => 'value']); // Outputs data as an array

$status = new Status(ResponseType::XML); // Initializes the Status class with XML response type