PHP应用程序架构设计帮助

PHP应用程序架构设计帮助,php,web-applications,Php,Web Applications,我正在用PHP开发一个新的社交网络类型的应用程序。我想在OO中完成这一切,我不想使用现有的框架 我一直在研究许多不同的框架和库,以了解它们是如何实现MVC之类的功能的 到目前为止,我得到的是这样的东西 // All request are routed through index.php // index.php Get requested page from URI (ie; test.com/user/friends/page-12 ) $controller = User(); $con

我正在用PHP开发一个新的社交网络类型的应用程序。我想在OO中完成这一切,我不想使用现有的框架

我一直在研究许多不同的框架和库,以了解它们是如何实现MVC之类的功能的

到目前为止,我得到的是这样的东西

// All request are routed through index.php
// index.php

Get requested page from URI (ie; test.com/user/friends/page-12 )
$controller = User();
$controller_method = friends();
$page = 12; // for paging results
$id = ''; //id is empty in this example but some pages will have an ID number as well
所以理论上我会加载一个User类和friends()方法。这一切听起来都很简单,在一个基本的网站,但我正在建设的将是更复杂的,所以我不知道我到底应该做下一步。例如,在某些页面上,我将要求用户已经获得授权

因此,我应该包含一个用户朋友文件,而不是加载一个用户类和朋友方法,这样我可以有更多的事情发生?在这种情况下,它将加载一个用户文件,该文件可以调用用户类方法,并设置分页,进行身份验证和其他应该在该页面上的操作

另一个想法是,由于本例调用的是用户类,那么什么是用户类具有方法friends()、profile()、settings(),这些方法在调用时基本上只是路由到包含另一个文件,该文件将包含该页面的主要内容?
很抱歉,如果您在边做边学的过程中对这一点感到困惑,那么您可能必须首先设计一个总体ACL(访问控制列表)身份验证方案,默认情况下,该方案会包含在每个页面的index.php文件中。然后,所有控制器(如
User()
类)都需要使用ACL(例如,假设存在一个全局
$auth
变量,该变量是
auth()
类的成员,或者出错)

以下是一些结构代码,让您开始:

Auth.php:

class Auth() {
  function login($user, $pass) {
    // Log in a user
  }
  function logout($user) {
    // Log the user out
  }
  function isLoggedIn($user) {
    // Verify that the user is logged in
  }
  function isVerified($user, $action) {
    // Is $user allowed to do $action?
  }
}
Index.php:

require_once('Auth.php');
$auth = new Auth();
$controller = User();
// ....
User.php:

class User() {
  function __construct() {
    // Determine if Auth is set up
    global $auth;
    if (!isset($auth) || !is_a($auth, 'Auth')) {
      return false; // Not properly set up for authentication
    }
  }
  function someSecretFunction($user, $password) {
    global $auth; // We know this exists; we checked it when creating the object
    if (!isset($auth) || !is_a($auth, 'Auth')) {
      return false; // Verify that it hasn't changed into something else since we checked
    }
    if ($auth->isVerified($user, 'someSecretFunction')) { // Use ACL functions now that we know we have them
      // ...
    }

  }
}

正如您在实践中学习的那样,您可能必须首先设计一个总体ACL(访问控制列表)身份验证方案,默认情况下,该方案包含在每个页面的index.php文件中。然后,所有控制器(如
User()
类)都需要使用ACL(例如,假设存在一个全局
$auth
变量,该变量是
auth()
类的成员,或者出错)

以下是一些结构代码,让您开始:

Auth.php:

class Auth() {
  function login($user, $pass) {
    // Log in a user
  }
  function logout($user) {
    // Log the user out
  }
  function isLoggedIn($user) {
    // Verify that the user is logged in
  }
  function isVerified($user, $action) {
    // Is $user allowed to do $action?
  }
}
Index.php:

require_once('Auth.php');
$auth = new Auth();
$controller = User();
// ....
User.php:

class User() {
  function __construct() {
    // Determine if Auth is set up
    global $auth;
    if (!isset($auth) || !is_a($auth, 'Auth')) {
      return false; // Not properly set up for authentication
    }
  }
  function someSecretFunction($user, $password) {
    global $auth; // We know this exists; we checked it when creating the object
    if (!isset($auth) || !is_a($auth, 'Auth')) {
      return false; // Verify that it hasn't changed into something else since we checked
    }
    if ($auth->isVerified($user, 'someSecretFunction')) { // Use ACL functions now that we know we have them
      // ...
    }

  }
}

基本上,您必须列出希望在应用程序中提供的所有函数,然后决定控制器和函数。当你做用例图时,你很容易理解,例如一个用例图=一个控制器。为什么人们总是想重新发明轮子?我同意Ikke的观点。有什么特别的原因让你不想使用现有的框架吗?@s992是的,我知道最好买一个自己的plus。经过两年的努力,尝试了12个不同的框架,我还没有找到一个我真正喜欢的框架yet@Ikke:因为控制盘并非适用于所有(甚至大多数,取决于您如何看待)用例。当然,一个给定的框架在这里可能更合适,但这并不意味着它更合适,也不意味着研究其他选项是错误的。。。有很多理由不使用现有的框架。因此,与其说只使用现有的框架,不如试着弄清楚海报是否考虑到了这一点,并根据手头任务的要求排除了它……基本上,您必须列出希望在应用程序中提供的所有功能,然后决定控制器和功能。当你做用例图时,你很容易理解,例如一个用例图=一个控制器。为什么人们总是想重新发明轮子?我同意Ikke的观点。有什么特别的原因让你不想使用现有的框架吗?@s992是的,我知道最好买一个自己的plus。经过两年的努力,尝试了12个不同的框架,我还没有找到一个我真正喜欢的框架yet@Ikke:因为控制盘并非适用于所有(甚至大多数,取决于您如何看待)用例。当然,一个给定的框架在这里可能更合适,但这并不意味着它更合适,也不意味着研究其他选项是错误的。。。有很多理由不使用现有的框架。因此,与其说只是使用现有的框架,不如试着弄清楚海报是否对此进行了研究,并根据手头任务的要求排除了它。。。