dongnan1989 2013-11-13 12:29
浏览 24
已采纳

会话对象应该在MVC架构中存在于何处?

I've build my own MVC framework, but I'm still getting my head around a few things.

I have a class to handle session methods such as refreshing sessions, verifying sessions, starting sessions, closing sessions.

My question is where should I declare it in my MVC architecture?

  • I have an abstract base class, which is extended by the controller.
  • This base class then declares the model and view.

Should it go in the base class? If so how can I refer to session object methods from the model? Or should I aim to only trigger it from the controller?

I'm new to Object Orientated php as well which makes this more hard. My mind is boggling!!

  • 写回答

3条回答 默认 最新

  • dongtangu8615 2013-11-13 13:08
    关注

    The request objects are best thought of, or at least written as, being part of the model layer, but because you're writing for the web, Request objects are part of the core of your Framework.
    But to clarify:

    Effectively, a Session class is a data model, and one could argue that it's part of a bigger subset of request models. Remember, MVC doesn't imply that each request requires a Controller, View and "A Model".
    The Model bit, especially, is a layer. It can contain services, data models, an ORM, helpers and what have you...

    That said, All things deal with the request should be at the ready in the controller. That's how most FW's work. Check the symfony workflow of a request, and note how far apart the controller and the request are.
    This graph, though it deals with the ZendFW cycle, shows the special status of the request object(s) even more clearly:

    ZFW request cycle

    The server receives the request, the framework kicks in and pours the request into the corresponding objects.
    Then, in the code that resolves the request to a given controller, and controller action, I'd pass the required request objects to the controller's constructor.
    A basic schematic would be:

    Request ===> index.php (startup script)
             ||
             || some basic checks, create objects
             ||
             |_==> create base controller
                           ||
                           ||
                           |_==> pass request to constructor, or starter method
                                 Do more specific/targetted checks
                                       ||
                                       ||
                                       \/
                            controller has access to request through base controller
                                   and can pass this through to the model layer
    

    So, in resuming, and because I believe repetition works: The request objects are best thought of, or at least written as, being part of the model layer, but because you're writing for the web, Request objects are part of the core of your Framework.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?