iPHP 框架 API 参考手册
iPHP 框架中所有核心类的详细说明,包括类的方法列表和示例。
1. 核心类列表
类名 |
文件路径 |
功能描述 |
Application |
Application.php |
应用类,负责应用的初始化和运行。 |
Cache |
core/Cache.php |
缓存类,支持多种缓存引擎(如 Memcached、Redis、文件缓存)。 |
Captcha |
core/Captcha.php |
验证码生成类,支持多种验证码生成方式。 |
Cookie |
core/Cookie.php |
Cookie 管理类,用于处理 Cookie 的读写。 |
Crypt |
core/Crypt.php |
加密解密类,支持 AES 加密算法。 |
DB |
core/DB.php |
数据库类,基于 Eloquent ORM,支持 MySQL、SQLite 等数据库。 |
Etc |
core/Etc.php |
配置文件管理类,用于读取和管理 JSON 格式的配置文件。 |
File |
core/File.php |
文件处理类,提供文件读写、删除等功能。 |
Helper |
core/Helper.php |
助手类,提供一些常用的工具方法。 |
Request |
core/Request.php |
请求处理类,用于处理 HTTP 请求。 |
Security |
core/Security.php |
安全类,提供 XSS、CSRF 等安全防护功能。 |
Session |
core/Session.php |
会话管理类,用于处理用户会话。 |
TemplateLite |
core/TemplateLite.php |
模板引擎类,用于渲染视图。 |
Utils |
core/Utils.php |
工具类,提供一些常用的工具方法。 |
2. 核心类详细说明
2.1 Application
类
- 文件路径:
Application.php
- 功能描述:负责应用的初始化和运行。
方法列表
方法名 |
参数 |
返回值 |
功能描述 |
run() |
无 |
void |
启动应用。 |
init() |
无 |
void |
初始化应用配置。 |
示例
require 'bootstrap.php';
$app = new Application();
$app->init();
$app->run();
2.2 Cache
类
- 文件路径:
core/Cache.php
- 功能描述:提供缓存功能,支持多种缓存引擎(如 Memcached、Redis、文件缓存)。
方法列表
方法名 |
参数 |
返回值 |
功能描述 |
get($key) |
$key (string) |
mixed |
获取缓存数据。 |
set($key, $value, $expire) |
$key (string), $value (mixed), $expire (int) |
bool |
设置缓存数据。 |
delete($key) |
$key (string) |
bool |
删除缓存数据。 |
示例
$cache = new Cache();
$cache->set('key', 'value', 3600);
$value = $cache->get('key');
$cache->delete('key');
2.3 Captcha
类
- 文件路径:
core/Captcha.php
- 功能描述:验证码生成类,支持多种验证码生成方式。
方法列表
方法名 |
参数 |
返回值 |
功能描述 |
generate() |
无 |
string |
生成验证码图片。 |
validate($code) |
$code (string) |
bool |
验证用户输入的验证码。 |
示例
$captcha = new Captcha();
$image = $captcha->generate();
$isValid = $captcha->validate($_POST['code']);
2.4 Cookie
类
- 文件路径:
core/Cookie.php
- 功能描述:Cookie 管理类,用于处理 Cookie 的读写。
方法列表
方法名 |
参数 |
返回值 |
功能描述 |
set($name, $value, $expire) |
$name (string), $value (mixed), $expire (int) |
void |
设置 Cookie。 |
get($name) |
$name (string) |
mixed |
获取 Cookie 值。 |
delete($name) |
$name (string) |
void |
删除 Cookie。 |
示例
$cookie = new Cookie();
$cookie->set('user', 'John', 3600);
$user = $cookie->get('user');
$cookie->delete('user');
2.5 Crypt
类
- 文件路径:
core/Crypt.php
- 功能描述:加密解密类,支持 AES 加密算法。
方法列表
方法名 |
参数 |
返回值 |
功能描述 |
encrypt($data) |
$data (string) |
string |
加密数据。 |
decrypt($data) |
$data (string) |
string |
解密数据。 |
示例
$crypt = new Crypt();
$encrypted = $crypt->encrypt('secret');
$decrypted = $crypt->decrypt($encrypted);
2.6 DB
类
- 文件路径:
core/DB.php
- 功能描述:数据库类,基于 Eloquent ORM,支持 MySQL、SQLite 等数据库。
方法列表
方法名 |
参数 |
返回值 |
功能描述 |
query($sql) |
$sql (string) |
mixed |
执行 SQL 查询。 |
table($table) |
$table (string) |
mixed |
指定操作的数据表。 |
示例
$db = new DB();
$result = $db->table('users')->where('id', 1)->first();
2.7 Etc
类
- 文件路径:
core/Etc.php
- 功能描述:配置文件管理类,用于读取和管理 JSON 格式的配置文件。
方法列表
方法名 |
参数 |
返回值 |
功能描述 |
get($key) |
$key (string) |
mixed |
获取配置项的值。 |
set($key, $value) |
$key (string), $value (mixed) |
void |
设置配置项的值。 |
示例
$etc = new Etc();
$value = $etc->get('database.host');
$etc->set('database.host', 'localhost');
2.8 File
类
- 文件路径:
core/File.php
- 功能描述:文件处理类,提供文件读写、删除等功能。
方法列表
方法名 |
参数 |
返回值 |
功能描述 |
read($path) |
$path (string) |
string |
读取文件内容。 |
write($path, $content) |
$path (string), $content (string) |
bool |
写入文件内容。 |
delete($path) |
$path (string) |
bool |
删除文件。 |
示例
$file = new File();
$content = $file->read('path/to/file.txt');
$file->write('path/to/file.txt', 'Hello World');
$file->delete('path/to/file.txt');
2.9 Helper
类
- 文件路径:
core/Helper.php
- 功能描述:助手类,提供一些常用的工具方法。
方法列表
方法名 |
参数 |
返回值 |
功能描述 |
arrayGet($array, $key, $default) |
$array (array), $key (string), $default (mixed) |
mixed |
从数组中获取值。 |
isEmail($email) |
$email (string) |
bool |
验证是否为有效的电子邮件地址。 |
示例
$helper = new Helper();
$value = $helper->arrayGet(['key' => 'value'], 'key', 'default');
$isEmail = $helper->isEmail('test@example.com');
2.10 Request
类
- 文件路径:
core/Request.php
- 功能描述:请求处理类,用于处理 HTTP 请求。
方法列表
方法名 |
参数 |
返回值 |
功能描述 |
get($key) |
$key (string) |
mixed |
获取 GET 参数的值。 |
post($key) |
$key (string) |
mixed |
获取 POST 参数的值。 |
示例
$request = new Request();
$id = $request->get('id');
$name = $request->post('name');
2.11 Security
类
- 文件路径:
core/Security.php
- 功能描述:安全类,提供 XSS、CSRF 等安全防护功能。
方法列表
方法名 |
参数 |
返回值 |
功能描述 |
xssClean($data) |
$data (string) |
string |
清理 XSS 攻击内容。 |
csrfToken() |
无 |
string |
生成 CSRF Token。 |
示例
$security = new Security();
$cleanData = $security->xssClean($_POST['data']);
$token = $security->csrfToken();
2.12 Session
类
- 文件路径:
core/Session.php
- 功能描述:会话管理类,用于处理用户会话。
方法列表
方法名 |
参数 |
返回值 |
功能描述 |
set($key, $value) |
$key (string), $value (mixed) |
void |
设置会话值。 |
get($key) |
$key (string) |
mixed |
获取会话值。 |
delete($key) |
$key (string) |
void |
删除会话值。 |
示例
$session = new Session();
$session->set('user', 'John');
$user = $session->get('user');
$session->delete('user');
2.13 TemplateLite
类
- 文件路径:
core/TemplateLite.php
- 功能描述:模板引擎类,用于渲染视图。
方法列表
方法名 |
参数 |
返回值 |
功能描述 |
assign($key, $value) |
$key (string), $value (mixed) |
void |
分配变量到模板。 |
render($template) |
$template (string) |
void |
渲染模板。 |
示例
$template = new TemplateLite();
$template->assign('title', 'Hello World');
$template->render('index.tpl');
2.14 Utils
类
- 文件路径:
core/Utils.php
- 功能描述:工具类,提供一些常用的工具方法。
方法列表
方法名 |
参数 |
返回值 |
功能描述 |
generateRandomString($length) |
$length (int) |
string |
生成随机字符串。 |
isUrl($url) |
$url (string) |
bool |
验证是否为有效的 URL。 |
示例
$utils = new Utils();
$randomString = $utils->generateRandomString(10);
$isUrl = $utils->isUrl('https://example.com');
以上是 iPHP 框架的核心类及其方法的详细说明。