2011-02-14 104 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 
     // ... 
    } 

    } 
}