anax / session
Anax Session模块,对session的包装。
Requires
- php: ^7.2
Requires (Dev)
- anax/configure: ^2.0.0@alpha
- anax/di: ^2.0.0@alpha
- phpunit/phpunit: ^7
Suggests
- anax/configure: ^2.0.0@alpha
This package is auto-updated.
Last update: 2024-08-23 17:45:06 UTC
README
Anax Session模块,用于包装session并提供与session相关的有用辅助函数。
当在CLI环境中运行时,Session模块已准备就绪,以便进行单元测试。
目录
类、接口、特性
以下类、接口和特性存在。
异常
没有特定于模块的异常。
配置文件
这是一个示例配置文件,通常存储在config/session.php
中。
/** * Config-file for sessions. */ return [ // Session name "name" => preg_replace("/[^a-z\d]/i", "", __DIR__), //"name" => preg_replace("/[^a-z\d]/i", "", ANAX_APP_PATH), ];
DI服务
session作为框架服务在$di
中创建。以下是如何创建session服务的示例。
/** * Creating the session as a $di service. */ return [ // Services to add to the container. "services" => [ "session" => [ "active" => defined("ANAX_WITH_SESSION") && ANAX_WITH_SESSION, // true|false "shared" => true, "callback" => function () { $session = new \Anax\Session\Session(); // Load the configuration files $cfg = $this->get("configuration"); $config = $cfg->load("session"); // Set session name $name = $config["config"]["name"] ?? null; if (is_string($name)) { $session->name($name); } $session->start(); return $session; } ], ], ];
session始终处于活动状态,在默认配置文件中,这取决于PHP定义ANAX_WITH_SESSION
,通常在config/commons.php
中设置,该文件由模块anax/commons
提供。
当ANAX_WITH_SESSION
被定义为true
时,session始终启动。
这是回调工作的方式,当它创建session服务时。
- 对象被创建。
- 读取配置文件,通常是
config/session.php
。 - 为session命名。
- 创建session。
服务是按需加载的,直到使用时才创建。然而,当"active" => true
时,它总是被加载。
作为框架服务访问
您可以将模块作为框架服务访问。
# $app style $app->session->start(); # $di style, two alternatives $di->get("session")->start(); $session = $di->get("session"); $session->start();
启动session
推荐的方法,也是默认行为,是通过定义ANAX_WITH_SESSION
并将其设置为true
来启动session。这通常在config/commons.php
中完成,其源代码在模块anax/commons
中可用。
将ANAX_WITH_SESSION
设置为false
表示默认情况下不会启动session。
作为替代方案,您可以在任何地方通过激活(使用)di服务来启动session。一个好的地方是在frontkontroller index.php
中,在创建$di
(和$app
)之后。然后,session将对所有页面请求可用。
在CLI(例如PHPUnit)运行时,session不会启动,但是它仍然处于活动状态,并且可以作为单元测试的手段使用。
处理session变量
当读取和设置session中的值时,可以使用辅助函数。
# Check if a key is set in the session $app->session->has($key); # Get a value from the session, null if it is not set. $app->session->get($key); # Get a value from the session or get the default value. $app->session->get($key, $default); # Set a value in the session, associated with a key. $app->session->set($key, $value); # Delete a key from the session. $app->session->delete($key);
session闪存消息
对于闪存消息,有一个有用的功能。从一个session中检索一个值,然后删除它。
# Get the value from the session and then delete its key, default is null. $app->session->getOnce($key); # Specify your own default value when key does not exists. $app->session->getOnce($key, $default);
调试session
您可以var_dump session对象,它将打印出$_SESSION
的内容。
# Debug the session and review its content through var_dump. var_dump($app->session);
销毁session
您可以销毁session,这在开发期间可能很有用。
# Destroy the session. $app->session->destroy();
许可证
本软件采用MIT许可证。有关详细信息,请参阅LICENSE.txt。
.
..: Copyright (c) 2013 - 2019 Mikael Roos, mos@dbwebb.se