donglian6625 2011-02-14 20:49
浏览 32
已采纳

PHP应用程序架构设计帮助

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

  • 写回答

1条回答 默认 最新

  • douyan1970 2011-02-14 21:59
    关注

    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
          // ...
        }
    
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口