企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] # 在session中存储数据 session组件提供面向对象的包装器来访问session数据。 使用此组件而不是原生session的原因: * 您可以轻松地在同一域上的应用程序之间隔离session数据 * 拦截在您的应用程序中设置/获取session数据的位置 * 根据应用程序需要更改session适配器 ## 启动Session 某些应用程序是session密集型的,几乎所有执行的操作都需要访问session数据。还有其他人随便访问session数据。感谢服务容器,我们可以确保只在明确需要时访问session: ```php <?php use Phalcon\Session\Adapter\Files as Session; // Start the session the first time when some component request the session service $di->setShared( 'session', function () { $session = new Session(); $session->start(); return $session; } ); ``` ## 工厂 使用`adapter`选项加载会话适配器类 ```php <?php use Phalcon\Session\Factory; $options = [ 'uniqueId' => 'my-private-app', 'host' => '127.0.0.1', 'port' => 11211, 'persistent' => true, 'lifetime' => 3600, 'prefix' => 'my_', 'adapter' => 'memcache', ]; $session = Factory::load($options); ``` ## 在Session中存储/获取数据 从控制器,视图或任何其他继承`Phalcon\Di\Injectable`的组件,您可以访问session服务并存储项目并按以下方式获取它们: ```php <?php use Phalcon\Mvc\Controller; class UserController extends Controller { public function indexAction() { // Set a session variable $this->session->set('user-name', 'Michael'); } public function welcomeAction() { // Check if the variable is defined if ($this->session->has('user-name')) { // Retrieve its value $name = $this->session->get('user-name'); } } } ``` ## 删除/销毁Session 它也可以删除特定变量或销毁整个session: ```php <?php use Phalcon\Mvc\Controller; class UserController extends Controller { public function removeAction() { // Remove a session variable $this->session->remove('user-name'); } public function logoutAction() { // Destroy the whole session $this->session->destroy(); } } ``` ## 隔离应用程序之间的session数据 有时,用户可以在同一session中在同一服务器上使用同一应用程序两次。当然,如果我们在session中使用变量,我们希望每个应用程序都有单独的session数据(即使相同的代码和相同的变量名称)。要解决此问题,您可以为在特定应用程序中创建的每个session变量添加前缀: ```php <?php use Phalcon\Session\Adapter\Files as Session; // Isolating the session data $di->set( 'session', function () { // All variables created will prefixed with 'my-app-1' $session = new Session( [ 'uniqueId' => 'my-app-1', ] ); $session->start(); return $session; } ); ``` 不需要添加唯一ID。 ## Session Bags `Phalcon\Session\Bag` 是一个帮助将会话数据分离到`命名空间`的组件。通过这种方式工作,您可以轻松地在应用程序中创建会话变量组。只需在包中设置变量,它就会自动存储在会话中: ```php <?php use Phalcon\Session\Bag as SessionBag; $user = new SessionBag('user'); $user->setDI($di); $user->name = 'Kimbra Johnson'; $user->age = 22; ``` ## 组件中的持久数据 继承 `Phalcon\Di\Injectable` 的控制器,组件和类可以注入`Phalcon\Session\Bag`。此类隔离每个类的变量。多亏了这一点,您可以以独立的方式在每个类中的请求之间保留数据。 ```php <?php use Phalcon\Mvc\Controller; class UserController extends Controller { public function indexAction() { // Create a persistent variable 'name' $this->persistent->name = 'Laura'; } public function welcomeAction() { if (isset($this->persistent->name)) { echo 'Welcome, ', $this->persistent->name; } } } ``` 在一个组件中: ```php <?php use Phalcon\Mvc\User\Component; class Security extends Component { public function auth() { // Create a persistent variable 'name' $this->persistent->name = 'Laura'; } public function getAuthName() { return $this->persistent->name; } } ``` 添加到会话的数据(`$this->session`)在整个应用程序中都可用,而持久性(`$this->persistent`)只能在当前类的范围内访问。 ## 实现自己的适配器 必须实现`Phalcon\Session\AdapterInterface` 接口才能创建自己的会话适配器或扩展现有会话适配器。 [Phalcon Incubator](https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Session/Adapter) 中有更多适用于此组件的适配器