PHP code example of cmrweb / unity_web_request

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

    

cmrweb / unity_web_request example snippets


namespace App\Service;

use Cmrweb\UnityWebRequest\UnityRequest;
use Symfony\Component\HttpFoundation\JsonResponse; 

class UnityRequestHandlerService extends UnityRequest
{ 
    public function handleSuccess(object $data): JsonResponse
    {
        ###> do something ###
        if(!isset($data->field)) {
            return $this->handleError('invalid field is missing');
        }
        if(empty($data->field)) {
            return $this->handleError('field cannot be empty'); 
        }
        $data->responseField = 'data added from server';
        ###< do something ### 
        return new JsonResponse(['Data'=>$data]);
    }

    public function handleError(string $message): JsonResponse
    {
        return new JsonResponse(['Error' => $message]);
    }
}


    #[Route('/unity/request', name: 'app_unity_request')]
    public function index(Request $request, UnityRequestHandlerService $unityRequestHandler): Response
    { 
        return $unityRequestHandler->handleRequest($request);
    }