anax/session 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

anax/session

Composer 安装命令:

composer require anax/session

包简介

Anax Session module, a wrapper around sessions.

README 文档

README

Latest Stable Version Join the chat at https://gitter.im/canax/session

Build Status CircleCI

Build Status Scrutinizer Code Quality Code Coverage

Maintainability Codacy Badge

Anax Session module, for wrapping the session and providing useful helpers related to the session.

The Session module is prepared wo be used for unit testing when run in CLI environment.

Table of content

Class, interface, trait

The following classes, interfaces and traits exists.

Class, interface, trait Description
Anax\Session\Session Wrapper class for sessions.
Anax\Session\SessionInterface Interface to implement for classes what want to wrap the session.

Exceptions

There are no module specific exceptions.

Configuration file

This is a sample configuration file, it is usually stored in 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 service

The session is created as a framework service within $di. The following is a sample on how the session service is created.

/**
 * 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;
            }
        ],
    ],
];

The session can always be active, in the default configuration file this is depending on the PHP define ANAX_WITH_SESSION which is usually set in config/commons.php which is available from the module anax/commons.

The session is always started when ANAX_WITH_SESSION is defined and true.

This is how the callback works, when it creates the session service.

  1. The object is created.
  2. The configuration file, usually config/session.php, is read.
  3. The session is named.
  4. The session is created.

The service is lazy loaded and not created until it is used. However, it is always loaded when "active" => true.

Access as framework service

You can access the module as a framework service.

# $app style
$app->session->start();

# $di style, two alternatives
$di->get("session")->start();

$session = $di->get("session");
$session->start();

Start the session

The recommended way, and the default behaviour, is to start the session by defining ANAX_WITH_SESSION and set it to true. This is usually done in config/commons.php which source is available in the module anax/commons.

Setting ANAX_WITH_SESSION to false means that the session is not started by default.

As an alternative, you can start the session anywhere by activating (using) the di service. A good place to do this is in the frontkontroller index.php, after you have created $di (and $app). The session will then be available for all page requests.

The session will not start when running in CLI (for example PHPUnit), however, it will still be active and usable as means of unit testing.

Work with session variables

There are helpers to use when reading and setting values in the 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 flash messages

There is feature useful for flash messages. A value is retrieved from the session and is then deleted.

# 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);

Debug the session

You can var_dump the session object and it will print out the content of $_SESSION.

# Debug the session and review its content through var_dump.
var_dump($app->session);

Destroy the session

You can destroy the session which might be useful during development.

# Destroy the session.
$app->session->destroy();

License

This software carries a MIT license. See LICENSE.txt for details.

 .  
..:  Copyright (c) 2013 - 2019 Mikael Roos, mos@dbwebb.se

anax/session 适用场景与选型建议

anax/session 是一款 基于 Makefile 开发的 Composer 扩展包,目前已累计 19.48k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 05 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「mvc」 「boilerplate」 「micro」 「education」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 anax/session 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 anax/session 我们能提供哪些服务?
定制开发 / 二次开发

基于 anax/session 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19.48k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 13
  • 依赖项目数: 30
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 2
  • 开发语言: Makefile

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-05-04