Session & Cookie
写入 Cookie:
方法和php原生的 set_cookie 一致。
Session
要使用session的话首先需要添加中间件 Cabal\Core\Http\Middleware\EnableSession
,在routes.php 中加入以下代码:
use Cabal\Core\Http\Middleware\EnableSession;
//...
Session 持久化存储依赖缓存服务,所以还要修改 usr/boot.php
,取消 Boot
类中的 use Cabal\Core\Cache\ServerHasCache
注释:
$response = new Response();
$session = $request->session();
if ($request->has('username')) {
// 写入
}
$response->getBody()->write(
isset($session['username']) ? ('已登录: ' . $session['username']) : '未登录'
);
$request->session()
返回的是一个实现了 \Iterator
, \ArrayAccess
, \Countable
, \JsonSerializable
的 Cabal\Core\Session
对象,所以你可以和使用 $_SESSION
一样使用它,也可以作为一个对象使用。
?> enableSession
中间件会在请求结束前将session中的数据持久化。