1. Go to this page and download the library: Download flexagon/framework 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/ */
flexagon / framework example snippets
use _Flexagon\Models\DataSourceModel;
_Global::$DATA_SOURCES['default'] = new DataSourceModel(
'localhost', // host
3306, // port
'username',
'password',
'dbname'
);
_Global::$DATA_SOURCES['default'] = new DataSourceModel('db-master', 3306, 'app', '****', 'service');
_Global::$DATA_SOURCES['analytics'] = new DataSourceModel('db-replica', 3306, 'ro', '****', 'stats');
namespace ExampleUser;
use _Flexagon\Base\BaseModel;
class UserModel extends BaseModel {
private ?int $id = null;
private string $name;
private ?string $address = null;
public function getId(): ?int { return $this->id; }
public function setId(?int $id): void { $this->id = $id; }
public function getName(): string { return $this->name; }
public function setName(string $name): void { $this->name = $name; }
public function getAddress(): ?string { return $this->address; }
public function setAddress(?string $a): void { $this->address = $a; }
}
namespace ExampleUser;
use _Flexagon\Base\BaseMySqlDAO;
class UserDAO extends BaseMySqlDAO {
protected string $tableName = 'users';
public function insert(UserModel $user): int|false { return $this->_insert($user); }
public function update(UserModel $user): bool { return $this->_update($user); }
public function delete(UserModel $user): bool { return $this->_delete($user); }
public function findById(int $id): ?UserModel {
return $this->_select(['id' => $id], new UserModel());
}
/** @return UserModel[] */
public function findByCity(string $city, int $page = 1, int $perPage = 20): array {
return $this->_selectList(
new UserModel(), $page, $perPage,
'`address` = :CITY', ['CITY' => $city],
'`created_at` DESC'
);
}
public function countByCity(string $city): int {
return $this->_selectTotalCount('`address` = :CITY', ['CITY' => $city]);
}
}
public function findTopSpenders(int $limit): array {
$this->db->executeQuery(
'SELECT u.* FROM users u JOIN orders o ON o.user_id = u.id
GROUP BY u.id ORDER BY SUM(o.amount) DESC LIMIT :LIMIT',
['LIMIT' => $limit]
);
return $this->db->getAllResultAsObject(new UserModel());
}
$this->_selectList(new UserModel(), 1, 20,
'`name` LIKE :KEYWORD OR `address` LIKE :KEYWORD', // may appear more than once
['KEYWORD' => "%{$keyword}%"]
);
echo $userDAO->debugQuery(); // actual SQL with parameters substituted
echo $userDAO->getError(); // last error message
$userDAO->debugDumpParams(); // PDOStatement::debugDumpParams()
// application/_Config.php
_Global::$SESSION_AUTO_START = true;
_Global::$SESSION_ENCRYPTION_STRING = 'change-this-to-a-long-random-string';
_Global::$SESSION_DOMAIN = '.example.com';
_Global::$SESSION_TIMEOUT_SECONDS = 86400;
// Cookie security attributes — the defaults are already secure and usually do not need to be changed.
_Global::$SESSION_COOKIE_SECURE = null; // null = automatically enabled for HTTPS requests
_Global::$SESSION_COOKIE_HTTPONLY = true; // prevent JavaScript from accessing the session cookie
_Global::$SESSION_COOKIE_SAMESITE = 'Lax';
namespace Session;
use _Flexagon\Base\BaseSessionDAO;
class SessionDAO extends BaseSessionDAO {}
// 로그인
$sessionUser = new SessionUserModel();
$sessionUser->setUsername($user->getUsername());
$sessionUser->setRole('ADMIN');
$sessionDAO = new SessionDAO();
$sessionDAO->makeSession($sessionUser);
// 로그아웃
$sessionDAO->cleanSession();
_Global::$SESSION_MODEL; // always available here
_Global::$SESSION_USER_MODEL; // class-name-based global (see below)
class _Global extends BaseGlobal {
public static ?\Session\SessionUserModel $SESSION_USER_MODEL = null;
}
// public/_prepare.php
if (str_starts_with(_Global::$URL_PARAM->filePath, 'admin/')) {
if (_Global::$SESSION_USER_MODEL?->getRole() !== 'ADMIN') {
\_Flexagon\Libs\HtmlUtil::redirectPage('/login');
}
}
// CSRF — no token mechanism is provided. Validate state-changing requests yourself.
// Output escaping — templates do not provide automatic escaping.
echo htmlspecialchars($user->getName(), ENT_QUOTES, 'UTF-8');
// Mass assignment — passing $_POST directly may overwrite fields such as id and role.
$user->setByArray(array_intersect_key($_POST, array_flip(['name', 'email'])));
// Serialization — getArray()/getJson()
TemplateLoader::show('head', ['title' => 'Product List']); // public/_Template/head.php
TemplateLoader::content(); // execute the page mapped to the URL
TemplateLoader::entryDir('_layout'); // _layout.php in the current URL directory
TemplateLoader::entryRoot('_footer'); // public/_footer.php
use _Flexagon\Libs\AssetLoader;
AssetLoader::setVersion('3.4.13'); // append ?_=3.4.13 to all asset URLs
AssetLoader::setAssetRootPath('/assets/');
AssetLoader::setCss('main.css'); // /assets/css/main.css (Loader::getAssetUrl('fonts/pretendard.woff2');
use _Flexagon\Supporters\EnumSupporter;
enum UserRole: string {
use EnumSupporter;
case ADMIN = 'ADMIN';
case USER = 'USER';
}
UserRole::keys(); // ['ADMIN', 'USER']
UserRole::values(); // ['ADMIN', 'USER']
UserRole::toArray(); // ['ADMIN' => 'ADMIN', 'USER' => 'USER']
UserRole::toCommaString(); // 'ADMIN,USER'
UserRole::toQuotedCommaString(); // "'ADMIN','USER'"
UserRole::toQuotedCommaString('"'); // '"ADMIN","USER"'
// Safe even when values contain quotes or commas
// case A = "it's"; case B = 'a,b';
// toCommaString() → it's,a,b (breaks DDL)
// toQuotedCommaString() → 'it''s','a,b'