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

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

问题描述

在编写 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.

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

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