问题描述
在编写 php 代码时,我决定从意大利面条式代码转向尝试实现 mvc.为了实现mvc框架,我发泄一下这篇文章文章开了个好头,我设法创建了我的网站,并开发了前端.现在,我正在尝试使用会话和其他会员区功能来实现后端.我的大脑因新信息而沸腾,我的问题多于答案.
while coding php, i decided to move from spaghetti code and try to implement mvc. to implement the mvc framework, i vent to this article article gave a good start and i managed to create my site, and develop the front-end. now, i am trying to implement the back-end using sessions and other member-area features. my brain is boiling with new information and i have more questions than answers.
我不知道如何实现额外的类,例如 user 类.例如,如果没有 mvc,我可以在我的包含目录中创建新的 user.php 类文件,然后包含它,实例化它,并为对象分配适当的值并将 objest 放入会话中.
i don't know how to implement additional classes, user class for example. for example, without mvc i could create new user.php class file in my includes directory, then include it, instantiate it, and assign appropriate values to the object and place objest into session.
我想寻求专家建议.
我对很多事情感到困惑:
i am confused about many things:
- 在哪里添加用户类
- 如何在我的 mvc 中添加和包含用户类
- 如何在我的应用程序中携带用户类(我在会话中理解,但会话必须具有用户对象的权限
- 我如何执行登录/注销逻辑并在后台执行所需的操作
这可能是一个常见的问题,一旦解决过就不复杂了.我也为 不是很好定义的问题 道歉,但我提前感谢您的帮助.
it is probaly a common problem that is not complex once it has been done before. i also apologize for not very good defined question, but i appreciate your help in advance.
推荐答案
user,mvc 的上下文,是一个 域对象.然而,会话是一种存储介质(就像缓存、数据库或文件系统).当您需要在那里存储来自 user 实例的数据时,您可以使用某种类型的 数据映射器 去做.
user, context of mvc, is a domain object. however the session is a form of storage medium (just like cache, db or file-system). when you need to store data from user instance there, you use some type of data mapper to do it.
$user = $this->domainobjectfactory->build('user'); $user->setname('korben') ->setsurname('dallas'); if ( some_condition ) { $mapper = $this->datamapperfactory->create('session'); $mapper->store($user); }
此代码应该为会话和用户之间的交互提供一个极其简化的示例.
this code should provide an extremely simplified example for interaction between session and user.
作为域对象,user 实例应该在 中使用服务 并使用工厂进行初始化.在 mvc 的上下文中,服务是模型层中的结构,它处理应用程序逻辑.它们操纵和促进领域对象和存储抽象的交互.
as a domain object, the user instances should be used inside services and initialized using factories. in context of mvc, the services are structures in model layer, which deal with application logic. they manipulate and facilitate the interaction of domain object and storage abstractions.
您的所有类都应该使用自动加载器添加.您应该阅读关于 spl_autoload_register(),最好同时使用 命名空间.
all of your classes should be added using autoloader. you should read about use of spl_autoload_register(), preferably while using namespaces.
实例本身的初始化应该由工厂完成.这使您可以将代码与所述实例的类名分离.
the initialization of instance itself should be done by a factory. that lets you decouple your code from the class name of said instance.
你没有.
php 应用程序不会持久化.你有一个 http 请求,你用它做所有你需要的事情,响应被发送,应用程序被销毁.user 类的实例都将是短暂的.
php applications do not persists. you have an http request, yo do all the things you need with it, the response is sent and application is destroyed. the instances of user class will all be short-lived.
要在请求之间恢复当前用户,请在会话中存储标识符.不要在会话中转储整个对象.相反,在您从会话中获取用户标识符后,您可以从其他形式的存储中恢复其余的用户帐户详细信息(如果您甚至需要的话).
to recover the current user between requests you store an identifier in session. do not dump whole objects in session. instead, after you fetch user's identifier from session, you can recover the rest of user account details from other forms of storage (if you even need it).
这整个过程应该由某种识别服务"执行和管理;或认证服务"来自您的模型层.
this whole process should be preformed and managed by some sort of "recognition service" or "authentication service" from your model layer.
登录请求首先由控制器处理:
the login request is at first handled by controller:
public function postlogin( $request ) { $service = $this->servicefactory->create('recognition'); $service->authenticate( $request->getparameter('username'), $request->getparameter('password') ); }
该服务尝试验证用户的凭据,这会改变模型层的状态.然后,视图实例会查找该状态,并将您作为经过身份验证的用户重定向到登录页面,或者将您重定向回带有错误消息的登录页面.
the service tries to verify the user's credentials, which alters the state of model layer. the view instance then looks up that state and either redirects you to the landing page as authenticated user, or redirects you back to login page with an error message.
服务本身将通过工厂在模型控制器和视图之间共享.这意味着他们只会初始化每个服务一次,然后就可以重用它.类似的东西:
the service themselves would be shared between model controller and view via the factory. which means that they would initialize each service only once and then just reuse it. something along the lines of:
class servicefactory { private $cache = array(); public function create( $name ) { if ( array_key_exists($name, $this->cache) === false ) { $this->cache[$name] = new $name; } return $this->cache[$name]; } }
请记住,这是一个极其简化的示例.
just keep in mind that his is an extremely simplified example.
要进一步阅读,我建议您浏览此链接集.此外,您可能会发现这 3 个帖子有些用处:this、这个 和这个.
for further reading i would recommend you to go through this collection of links. also, you might find these 3 posts somewhat useful: this, this and this.