session-interop/utils.arraysession
最新稳定版本:3.0.0
Composer 安装命令:
composer require session-interop/utils.arraysession
包简介
This package contains the session interface implementation
README 文档
README
Array Session
This package is a basic implementation of SessionInterface
Installation
You can install this package through Composer:
composer require session-interop/utils.arraysession
The packages adheres to the SemVer specification, and there will be full backward compatibility between minor versions.
Available
SessionArray.
The implementation of SessionInterface.
Methods
__construct(&$array, $prefix = "")
Construct the object. The object will use $array to store element. It use a reference on $array, so $array will be modified when you use set($key)and remove($key).
If prefix is given, it will automatically prefix all $key.
has($key)
Verify is the key $key exists into the array injected at the object construction. Throw an exception if the key is not a string. if the associated value is null, the method return true.
get($key)
Get the value associated with $key. Throw an exception if no key are found or if the key is not a string
set($key, $val)
Set the value $val at the key $key. Throw an exception if the key is not a string.
remove($key)
Destroy the element at $key
SessionException.
Exception used on error. Throw an exception if the key is not a string
Usage
Writing an user service that use the session interface:
UserService.php
namespace Usage; use Interop\Session\SessionInterface; class UserService { public function isConnected(SessionInterface $session) { if ($session->has("userId")) { return true; } return false; } public function login(SessionInterface $session, $userId) { if ($this->isConnected($session)) { return false; } $session->set("userId", $userId); return true; } public function logoff(SessionInterface $session) { if ($this->isConnected($session)) { $session->remove("userId"); return true; } return false; } }
Use the implementation:
Index.php
use Interop\Session\Utils\ArraySession\ArraySession; use Usage\UserService; require_once("vendor/autoload.php"); session_start(); $userId = 1; $userService = new UserService(); $session = new ArraySession($_SESSION, "myprefix"); // Check if the user is connected if ($userService->isConnected($session)) { // logoff the user $userService->logoff($session); } else { // login the user $userService->login($session, $userId); }
统计信息
- 总下载量: 53.62k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-06-06