PHP code example of william-nahirnei / bagual-php
1. Go to this page and download the library: Download william-nahirnei/bagual-php 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/ */
william-nahirnei / bagual-php example snippets
protected ?string $defaultAuthClass = TokenAuth::class;
protected ?string $defaultAuthMethod = "authenticate";
public function __construct() {
parent::__construct(
$this->moduleName,
$this->defaultAuthClass,
$this->defaultAuthMethod,
$this->ignoreAuth
);
}
$this->addEndpoint(static::METHOD_GET, null, UsuarioController::class, "listar");
namespace Src\Modules\Usuario;
use Server\Routing\AbstractApi;
use Src\Modules\Usuario\Auth\TokenAuth;
use Src\Modules\Usuario\Controller\UsuarioController;
class Api extends AbstractApi {
protected ?string $moduleName = "usuario";
protected ?string $defaultAuthClass = null;
protected ?string $defaultAuthMethod = null;
protected ?bool $ignoreAuth = false;
public function __construct() {
parent::__construct(
$this->moduleName,
$this->defaultAuthClass,
$this->defaultAuthMethod,
$this->ignoreAuth
);
}
public function defineEndpointList(): void {
$this->addEndpoint(static::METHOD_GET, null, UsuarioController::class, "listar", TokenAuth::class, "authenticate");
$this->addEndpoint(static::METHOD_POST, null, UsuarioController::class, "criar", TokenAuth::class, "authenticate");
$this->addEndpoint(static::METHOD_PUT, null, UsuarioController::class, "atualizar", TokenAuth::class, "authenticate");
$this->addEndpoint(static::METHOD_DELETE, "deletar", UsuarioController::class, "deletar", TokenAuth::class, "authenticate");
$this->addEndpoint(static::METHOD_GET, "publico", UsuarioController::class, "publico");
}
}
$parametrosQueryString = Request::getInstance()->getQueryParams();
$parametrosBody = Request::getInstance()->getBodyParams();
$todosParametros = Request::getInstance()->getAllMergedParams();
$headers = Request::getInstance()->getHeaders();
Response::setStatusCode(StatusCodes::HTTP_OK);
Response::addHeader(Response::HEADER_CONTENT_TYPE, Response::CONTENT_TYPE_JSON);
namespace Src\Auth;
use Server\Auth\AbstractAuthenticable;
use Server\Constants\ServerMessage;
use Server\Errors\AuthenticationException;
class GeneralAuth extends AbstractAuthenticable{
/**
* This method must be implemented by subclasses and must throw AuthenticationException
*
* @throws AuthenticationException
*/
protected static function callAuthError(): void {
throw new AuthenticationException([ServerMessage::DEFAULT_AUTH_ERROR]);
}
public static function authenticate() {
return false;
}
}
protected ?string $defaultAuthClass = TokenAuth::class;
protected ?string $defaultAuthMethod = "authenticate";
$this->addEndpoint(static::METHOD_GET, null, UsuarioController::class, "listar", TokenAuth::class, "authenticate");
throw new ApiException(true, ApiExceptionTypes::ERROR, ["Usuario não encontrado"], StatusCodes::HTTP_NOT_FOUND);
protected const FILE_NAME = '.exemplo.env';
protected const CONFIG_KEYS = ["CFG_EXEMPLO"];
namespace Src\Modules\Usuario;
use Config\ConfigLoader;
class ConfigExemplo extends ConfigLoader {
protected const FILE_NAME = '.exemplo.env';
public const CFG_EXEMPLO = "CFG_EXEMPLO";
protected const CONFIG_KEYS = [
self::CFG_EXEMPLO,
];
}
ConfigExemplo::getInstance()->getConfig("Nome da configuração definida no arquivo .env");
bash
composer create-project william-nahirnei/bagual-php nome-projeto
bash
php -S localhost:8000
Config/ConfigLoader.php