开发者

PHP application architecture design help

I am working on a new social network type app in PHP. I am wanting to do it all in OO and I do not want to use an existing framework.

I have been studying many different frameworks and libraries to see how they do things like MVC.

So far what I have is something like this...

// 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

So in theory I would load a User class and friends() method. This all sounds simple and great on a basic site but what I am building will be more complex so I am not sure exactly what I should do next. For example on some pages, I will require that a开发者_C百科 user is authorized already.

So instead of loading a User class and friends method, should I be including a user friends file instead where I can have more stuff happening? In this case it would load a user file and that file could call user class methods as well as set up paging and do authentication and other things that should be on that page.

Another idea, since this example is calling the user class, what is the user class has methods friends() , profile(), settings() and these methods when called basicly just route to include another file with that will have the main content for that page? Sorry if this is confusing


As you're learning by doing, you'll likely have to start with designing an overarching ACL (access control list) authentication scheme that gets included by your index.php file by default for every page. Then all controllers (like your User() class) need to make use of the ACL (say, by assuming there's a global $auth variable, that's a member of your Auth() class, or error out).

Here's some structure code to get you started:

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
      // ...
    }

  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜