2011-02-14 87 views
1

我正在使用PHP中的新社交网络类型应用程序。我想在OO中完成所有工作,而且我不想使用现有的框架。PHP应用程序体系结构设计帮助

我一直在研究许多不同的框架和库,看看他们如何做像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(),profile(),settings()和这些方法时调用基本上只是路由包含另一个文件将有该页面的主要内容? 对不起,如果这是令人困惑

+0

基本上你将不得不列出所有的功能t你想在你的应用程序中提供的帽子,然后决定控制器和功能。当您使用案例图时,您可以轻松理解,例如,一个用例图=一个控制器。 – tHeSiD 2011-02-14 21:03:19

+3

为什么人们总是想重新发明轮子? – Ikke 2011-02-14 21:19:19

回答

1

正如你在做的学习,你可能不得不开始设计一个总体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 
     // ... 
    } 

    } 
}