我正在使用哪种架构模式?

Which architectural pattern am I using?

我通过 PHP 在不使用任何框架的情况下开发了 Web 应用程序。我的应用程序主要有两种类型的文件——前端和后端。第一种类型可能包含 HTML、PHP、CSS、JavaScript (jQuery) 和后端 - 只有 PHP。我有一个名为 pg_db_connection 的 class 与数据库建立连接,还有一个 class session 创建用户的网络会话(php 的函数 session_start()) 并维护一些变量,如 "username"、users 数据库中的用户 ID table 等

pg_db_connection class 有一个 属性 $link 这是从 pg_connect() 获得的数据库资源。此 class 还具有一些功能,如 query($query, $b_result = false, &$affected_rows = null)insert($table, $values, $columns = null, &$affected_rows = null)begin()commit()rollback() 等。在每个前端文件的开头,我创建 session 类型的对象并执行:

$db = new pg_db_connection($db_config,$log_mng);
$session = new session($db);

#if the session is not active go to login.php frontend and force the user to login
if(!$session->is_active())
{
    header("Location: /html/admin/login.php?url=" . urlencode($_SERVER['REQUEST_URI']));
    exit;
}


# If session is active proceed below
# Auto refresh the session
$session->autoReresh();

# Check if the current user have privileges to access this frontend file (second param is file path, third - file name)
if(!($session->passpermit($session->user_id(), $_SERVER['SERVER_ADDR'], dirname(__FILE__)."/", basename(__FILE__))))
{
    header("Location: /html/admin/access_denied.html");
    exit;
}

会话 class 在 $_SESSION 中存储 user_id, username 以及更多内容。需要连接到数据库,因为web用户有权访问的文件存储在数据库中。如果我想在此前端文件中加载任何动态数据,我使用 jQuery 的 postload 函数并调用一个后端文件。在大多数情况下,此后端文件包括 pg_db_connection,执行一些数据库查询,如果还需要 - 对数据做更多的工作(用 HTML 标记包装,或以某种方式格式化数组然后 json_encode 它),然后检索 HTML 或 JSON 到前端文件。然后在 jquery 的加载或 post 回调方法中,这个 HTML 写在需要的地方,或者 JSON 以某种方式转换为 HTML 并再次写在某个地方在 HTML。

我想知道我是否使用了任何一种已知的架构模式。或者哪种架构模式最接近描述的方法?

据我所知,您的应用程序架构并未具体遵循任何特定的架构模式。 通常,您使用 客户端(前端)- 服务器(后端)架构 并使用 JavaScript/Ajax 请求从前端获取数据。 您使用不指定业务逻辑代码的体系结构...因此无法判断您是否使用 MVC 模式等... 查看此 link 以了解更多信息: https://softwareengineering.stackexchange.com/questions/158260/is-there-any-design-pattern-except-mvc-for-web

我还建议您阅读这篇文章,以更好地理解 Web 应用程序设计决策: Web Application Design Patterns

根据本 link 中的列表,我认为您使用以下设计模式:

  • Request-Processing:页面控制器(显然您有一个条目 class 控制身份验证和授权)

  • Presentation:监督 Presenter(如果我没猜错的话,你在服务器中执行主要逻辑,但你随后委派了一些 UI/JSON-Content前端的替换任务等 JavaScript)

  • 页面布局 (UI): Transform/Two-Step 视图(您使用 jQuery 创建一些 HTML 出 JSON 对吧?)

  • Persistence:事务数据存储(因为您使用了 begin()、commit()、rollback())

一些批评: pg_db_connection 暗示你必须使用 postgres DB 我猜? ...因此您无法轻松切换数据库...并且您必须处理容易出错且存在安全风险的低级别 SQL 查询... 自定义会话处理也很容易出错,有很多陷阱...... 例如

header("Location: /html/admin/login.php?url=" . urlencode($_SERVER['REQUEST_URI']))

...可能会导致重定向漏洞...我什至不想知道您在 login.php 中做了什么...

关于 exit; 你可能想在这里阅读为什么 exit() 是次优的: Best Practice for PHP exit()

无论如何,人们实际上没有从头开始编写自己的 Web 应用程序架构,而是受到启发或使用 PHP 框架是有原因的:

  • Make speed development possible
  • Provide well-organized, reusable and maintainable code
  • Let you grow over time as web apps running on frameworks are scalable
  • Spare you from the worries about low-level security of a site
  • Follow the MVC (Model-View-Controller) pattern that ensures the separation of presentation and logic
  • Promote modern web development practices such as object-oriented programming tools

查看此博客中介绍的一些当前现代框架 post: http://www.hongkiat.com/blog/best-php-frameworks/ (这也是前面提到的框架使用原因的来源......) 也许某些东西适合您的用例而不是太 slow/bloated/etc...