熟悉MVC-如何使用会话逻辑,其他类和后台逻辑 [英] Getting familiar with MVC - how do I work with session logic, additional classes and background logic

查看:58
本文介绍了熟悉MVC-如何使用会话逻辑,其他类和后台逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写PHP的同时,我决定从意大利面条式代码转而尝试实现MVC. 为了实现MVC框架,我尝试发表本文 Article提供了一个良好的开始,我设法创建了自己的网站并开发了前端. 现在,我正在尝试使用会话和其他成员区域功能来实现后端.我的大脑充满了新的信息,我的问题多于答案.

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,我可以在我的include目录中创建一个新的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中
  • 如何在应用程序中携带用户类(我在会话中理解,但是会话必须对用户对象有一点帮助
  • 我如何执行登录/注销逻辑并执行背景中发生的必要操作

很可能是一个常见的问题,一旦完成就不会复杂.我也对not very good defined question表示歉意,但感谢您的帮助.

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, 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篇文章有些有用:.

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.

这篇关于熟悉MVC-如何使用会话逻辑,其他类和后台逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆