简体   繁体   English

在MVC和OO PHP中使用会话的最佳方法

[英]Best way to use sessions with MVC and OO PHP

I've been working with sessions, MVC design and object oriented PHP. 我一直在使用会话,MVC设计和面向对象的PHP。 Where should I save or retrieve data from a session? 我应该在哪里保存或检索会话中的数据? I would like to retrieve it from within methods so I don't have to pass the data to the methods. 我想从方法内部检索它,因此不必将数据传递给方法。 Whats the best practice? 什么是最佳做法?

I typically put this inside the controller. 我通常将其放入控制器中。 It just makes sense.. The controller decides what happens and why not let it decide if people are allowed to do the requested actions. 这很有意义。控制器决定发生什么事情,为什么不让它决定是否允许人们执行所请求的动作。 Typically you have multiple controllers in a MVC system. 通常,MVC系统中有多个控制器。 Eg. 例如。 BaseController (abstract - common), NonSessionController extends BaseController (eg: used for static pages), SessionController extends BaseController (primary session handing here - this could be abstract). BaseController(抽象-通用),NonSessionController扩展BaseController(例如:用于静态页面),SessionController扩展BaseController(此处的主要会话处理-这可能是抽象的)。 If you have for example different user types, you may want to polymorph this controller ones such as: AdminController, UserController, Etc. 例如,如果您有不同的用户类型,则可能需要使该控制器的类型多态,例如:AdminController,UserController,Etc。

Personally, I'm a huge fan of the Zend_Session wrapper class. 就个人而言,我是Zend_Session包装器类的忠实粉丝。 I prefer to work with data in an object-oriented style and the namespacing advantage to using a wrapper is a huge plus. 我更喜欢以面向对象的方式处理数据,与使用包装器相比,命名空间优势是一个巨大的优势。

Which of these looks better to you? 您认为其中哪一个更好?

$_SESSION['Zend_Auth']['user'] = "myusername";

or 要么

$authNamespace = new Zend_Session_Namespace('Zend_Auth');
$authNamespace->user = "myusername";

I prefer the look that using the accessors gives you. 我更喜欢使用访问器给您的外观。

Note: In an MVC system, no matter what method you choose, you should always be getting/setting session data in your controller. 注意:在MVC系统中,无论选择哪种方法,都应该始终在控制器中获取/设置会话数据。

I've tried it a few ways, including using a static wrapper class to handle it, but I always come back to just using the superglobal array by itself. 我已经尝试了几种方法,包括使用静态包装器类来处理它,但是我总是回到单独使用超全局数组的角度。 I still use a wrapper for authentication checks and other repetitive tasks, but, ultimately, it's just easier and less verbose for me to use the stock setup. 我仍然使用包装器进行身份验证检查和其他重复性任务,但是,最终,使用库存设置对我来说更简单,更省力。

我认为这取决于所检索数据的使用范围,如果仅在方法内部使用,那么为什么要在外部检索数据,并且会话始终在超全局变量中可用,因此最好仅在需要时进行本地化。

I wouldn't bother with session wrappers. 我不会理会会话包装器。 You won't gain enough to merit the limitations. 您将不会获得足够的好处来享受这些限制。 Going through the superglobal allows you to use any sort of (hopefully sane) data structure you want. 通过超全局,您可以使用所需的任何类型的(希望是理智的)数据结构。 My session data always ends up being 2 or more levels of array data, which is too tedious to manage through a session wrapper. 我的会话数据始终最终是2级或更多级的数组数据,这太繁琐而无法通过会话包装器进行管理。

The superglobal doesn't even limit you from having PHP store your session data in a database using a save handler , which is quite nice for scalability. 超全局变量甚至没有限制您让PHP使用保存处理程序将会话数据存储在数据库中,这对于可伸缩性而言非常好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM