PHP code example of tomi20v / phalswag

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

    

tomi20v / phalswag example snippets


class UsersController extends Controller
{
	// by defining these,
	protected static $_swaggerPath = '../app/config/swagger';
	protected static $_swaggerFname = 'users.json';

	public function getAction() {

		try {

			$Response = $this->_process(
				// swagger operation ID
				'usersGet',
				// callback which receives input and shall return result data
				function($RequestModel) {
					$User = UserModel::findById($RequestModel->id);
					return $User;
				}
			);

		}
		catch (\Exception $e) {

			$Response = $this->ResponseBuilder->buildError(500);

		}

		return $Response;

	}
}

// get the operation object
$Operation = $this->SwaggerService->getOperationById(
	$operationId,
	$this->_Swagger
);

// bind to request model
$this->SwaggerService->bindRequest(
	$RequestModel,
	$Operation,
	$this->dispatcher->getParams(),
	$this->request
);

// get response schema and build from data object
$ResponseSchema = $this->SwaggerService->getResponseSchema(
	200,
	$Operation,
	$this->_Swagger
);
$Result = $this->SwaggerService->buildBySchema(
	$Object,
	$ResponseSchema
);