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
我正在使用PHP开发一个新的社交网络类型的应用程序。 我想在OO中完成所有操作,我不想使用现有的框架。 p>
我一直在研究许多不同的框架和库,看看他们是如何做的事情,比如MVC。 p>
到目前为止,我所拥有的是这样的。 .. p>
//所有请求都通过index.php
// index.php
路由来自URI的请求页面(即; test.com/user/) friends / page-12)
$ controller = User();
$ controller_method = friends();
$ page = 12; //用于分页结果
$ id =''; //在这个例子中id是空的,但是一些页面也会有一个ID号
code> pre>
所以理论上我会加载一个User类和friends()方法。 这在基本网站上听起来简单而且很棒,但我正在构建的内容会更复杂,所以我不确定接下来应该做些什么。 例如,在某些页面上,我将要求已经授权用户。 p>
所以我不应该加载一个User类和朋友方法,而应该包含一个用户朋友文件,而不是我可以发生更多的事情? 在这种情况下,它将加载一个用户文件,该文件可以调用用户类方法,也可以设置分页,并进行身份验证和应该在该页面上的其他内容。 p>
另一个想法, 因为这个例子是调用用户类,所以用户类有什么方法有friends(),profile(),settings()和这些方法,当被调用时,只是路由包含另一个文件,其中包含该页面的主要内容?
如果这令人困惑,请小心 p>
div>
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
// ...
}
}
}