架构
核心目录结构、请求生命周期,以及 TubePress 所遵循的无框架开发约定。
TubePress 是一款刻意保持简洁的软件。它没有框架、没有依赖注入容器、也没有构建步骤——只有使用静态类的、组织良好的现代 PHP。本页面介绍各模块如何协同工作,帮助你自信地扩展 CMS 并清楚地知道代码应放在何处。
设计理念
三个核心理念塑造了整个代码库:
- 无框架。 使用自定义静态类的纯 PHP。没有 Composer 运行时依赖可供破坏,除 PHP 本身外无需学习其他内容。
- 固定核心,自由扩展。 核心可升级,且不应直接编辑。所有自定义内容存放于主题、插件或设置中。
- 为可扩展性与低成本主机环境而构建。 繁重任务会被分块处理,内置的 pseudo-cron 意味着无需系统级 crontab。
目录结构
代码库将核心(固定、可升级)与扩展(由用户安装和移除)分离。
CORE — never modified by themes/plugins, replaced by updates
config/ App config, DB config, install marker
src/Core/ App, Database, Router, Pagination, CronManager,
and the managers (Seo, Transcode, Update, …)
src/Auth/ Auth, Gate (permissions)
src/Models/ Video, Category, Tag, Performer, Channel,
Comment, Page, User, Setting, Favorite, History
src/Admin/ Admin CRUD controllers
src/Front/ Public-facing controllers
src/Install/ InstallController, Migrator
src/Helpers/ Format, FileUpload, Sanitizer, Slug, SEO,
Player, RateLimiter, ImpressionTracker
src/Plugin/ HookSystem, PluginInterface, PluginManager
src/Theme/ ThemeManager, ThemeRenderer
sql/ Numbered migrations (001_…sql → 0xx_…sql)
templates/admin/ Admin panel templates (layout + partials)
templates/install/ Install wizard templates
public/ Web root — index.php, uploads/, symlinks
cron/ Optional CLI scripts for a system crontab
ADD-ONS — installable / removable by users
themes/ Theme directories (active: "simply")
plugins/ Plugin directories (agegate, backup, ctr)
lang/ Translation files (en, fr, de, es, ar, …)请求生命周期
每个请求都以相同顺序流经 public/index.php:
- PHP 版本守卫。
config/php-guard.php最先运行——这是一个解析安全的门卫,在加载任何现代语法之前,若 PHP 版本不受支持则阻止启动。 - 引导启动。
config/app.php配置自动加载、会话及核心服务。 - 安装检查。 若
App::isInstalled()返回 false(不存在config/installed.php),请求将被路由至安装向导,其他内容均不加载。 - 路由注册。 Admin、API 和前台控制器各自通过静态
::routes()方法注册路由。 - 调度。
Router::dispatch()匹配 URL 并调用相应控制器方法。 - 渲染。 Admin 操作渲染
templates/admin/*;前台操作通过ThemeRenderer::render()渲染当前活跃主题。 - 渲染后处理。 印象数据刷新,
CronManager::run()触发所有到期任务。
前台页面路由(静态页面的 /{slug} 全匹配)最后注册,因此不会遮蔽真实路由。
核心约定
| 概念 | 约定 |
|---|---|
| 控制器 | AdminXxxController::routes() / FrontXxxController::routes() 注册路由;方法处理请求。 |
| 模型 | 静态方法:Video::find($id)、Video::all($filters)、Video::create($data)。 |
| 数据库 | Database::fetchOne()、fetchAll()、insert()、update()、delete()、count()、transaction()。 |
| 路由器 | Router::get()、Router::post()、Router::param('name')、Router::redirect()。 |
| Admin 模板 | Router::render('admin/videos', […]) → templates/admin/videos.php(加载 layout.php 和 _videos.php 局部模板)。 |
| 前台模板 | ThemeRenderer::render('home', […]) → 活跃主题模板及其 layout.php。 |
| 迁移 | sql/NNN_name.sql,由 Migrator::run() 在安装和更新时自动执行。 |
| 钩子 | HookSystem::doAction('event') 和 HookSystem::applyFilter('filter', $value)。 |
Web 服务器支持
TubePress 同时支持 Apache 和 Nginx:
- Apache —
.htaccess文件处理 URL 重写和目录保护。 - Nginx —
alias指令提供静态资源服务,deny all规则保护敏感路径。 - 符号链接 —
public/themes → ../../themes和public/templates → ../../templates暴露主题和 Admin 静态资源,同时将 PHP 源码保留在 Web 根目录之外。
Web 根目录始终为 public/。所有敏感内容——config/、src/、sql/、storage/——均位于其外部,并在服务器层面额外加以拦截。
如何自定义
| 你想要…… | 在……中实现 |
|---|---|
| 更改外观与布局 | 主题(或无代码更改的外观设置) |
| 添加功能或集成 | 使用动作与过滤器的插件 |
| 无需代码更改行为 | 设置 |
| 添加计划任务 | 通过插件中的 CronManager |