We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
前言还没想好。
SessionServiceProvider 没有 boot 方法,其 register 方法为
SessionServiceProvider
boot
register
laravel/vendor/laravel/framework/src/Illuminate/Session/SessionServiceProvider.php
Lines 15 to 22 in ca57c28
作用
单例注册 Illuminate\Session\SessionManager 为 session
Illuminate\Session\SessionManager
session
单例注册 Illuminate\Session\Store 为 session.store
Illuminate\Session\Store
session.store
具体生成 Illuminate\Session\Store 对象的过程,由你 .env 配置 SESSION_DRIVER 而调用 Illuminate\Session\SessionManager 不同方法:
.env
SESSION_DRIVER
createArrayDriver
createCookieDriver
createFileDriver
createNativeDriver
createDatabaseDriver
createApcDriver
createMemcachedDriver
createRedisDriver
单例注册 Illuminate\Session\Middleware\StartSession 中间件
Illuminate\Session\Middleware\StartSession
下面,我们以 SESSION_DRIVER=file 继续分析 当 SESSION_DRIVER 为 file 时,会调用到 createFileDriver
SESSION_DRIVER=file
file
laravel/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php
Lines 47 to 50 in ca57c28
Lines 57 to 64 in ca57c28
这里 $this->app['config']['session.files'] 得到的是
$this->app['config']['session.files']
laravel/config/session.php
Line 60 in ca57c28
然后通过此处实例化 Illuminate\Session\FileSessionHandler
Illuminate\Session\FileSessionHandler
Line 61 in ca57c28
然后调用 buildSession 获得 \Illuminate\Session\Store 对象
buildSession
\Illuminate\Session\Store
Lines 163 to 170 in ca57c28
当触发 Session::start 时,
Session::start
laravel/vendor/laravel/framework/src/Illuminate/Session/Store.php
Lines 69 to 78 in ca57c28
调用的 loadSession 为
loadSession
Lines 85 to 88 in ca57c28
调用的 readFromHandler 为
readFromHandler
Lines 95 to 106 in ca57c28
这里的 read 方法,就是 FileSessionHandler::read
read
FileSessionHandler::read
laravel/vendor/laravel/framework/src/Illuminate/Session/FileSessionHandler.php
Lines 67 to 76 in ca57c28
执行到这里时, session 中的数据就被存放到了 Illuminate\Session\Store::$attributes 了。
Illuminate\Session\Store::$attributes
而执行 Session::get 时,
Session::get
Lines 205 to 208 in ca57c28
正好就能从 Store::$attributes 中取出 session 的数据了。
Store::$attributes
当我们使用 Illuminate\Support\Facades\Session 时,操作是调用到的 session,也就是 Illuminate\Session\SessionManager。但是在 Illuminate\Support\Manager 的 __call 魔术方法,实现了将 session.driver 载入到 session 中被执行的能力。
Illuminate\Support\Facades\Session
Illuminate\Support\Manager
__call
session.driver
laravel/vendor/laravel/framework/src/Illuminate/Support/Manager.php
Lines 144 to 147 in ca57c28
有兴趣的同学请阅读 15. Laravel 神奇的 Manager 类
假如我们执行 Session::put('key', 'value') 会穿透到
Session::put('key', 'value')
Lines 265 to 274 in ca57c28
当我们手动 Session::save 时,或者程序终止运行触发 register_shutdown_function 时,会触发
Session::save
register_shutdown_function
Lines 124 to 133 in ca57c28
也就是 FileSessionHandler::write
FileSessionHandler::write
Lines 81 to 87 in ca57c28
至此,写入 session 的流程走完。
The text was updated successfully, but these errors were encountered:
No branches or pull requests
服务提供者部分
SessionServiceProvider
没有boot
方法,其register
方法为laravel/vendor/laravel/framework/src/Illuminate/Session/SessionServiceProvider.php
Lines 15 to 22 in ca57c28
作用
单例注册
Illuminate\Session\SessionManager
为session
单例注册
Illuminate\Session\Store
为session.store
createArrayDriver
createCookieDriver
createFileDriver
createNativeDriver
其实createFileDriver
就是调用的此方法createDatabaseDriver
createApcDriver
createMemcachedDriver
createRedisDriver
单例注册
Illuminate\Session\Middleware\StartSession
中间件下面,我们以
SESSION_DRIVER=file
继续分析当
SESSION_DRIVER
为file
时,会调用到createFileDriver
laravel/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php
Lines 47 to 50 in ca57c28
createFileDriver
调用了createNativeDriver
laravel/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php
Lines 57 to 64 in ca57c28
这里
$this->app['config']['session.files']
得到的是laravel/config/session.php
Line 60 in ca57c28
然后通过此处实例化
Illuminate\Session\FileSessionHandler
laravel/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php
Line 61 in ca57c28
然后调用
buildSession
获得\Illuminate\Session\Store
对象laravel/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php
Lines 163 to 170 in ca57c28
session 功能 / 操作部分
读取 session
当触发
Session::start
时,laravel/vendor/laravel/framework/src/Illuminate/Session/Store.php
Lines 69 to 78 in ca57c28
调用的
loadSession
为laravel/vendor/laravel/framework/src/Illuminate/Session/Store.php
Lines 85 to 88 in ca57c28
调用的
readFromHandler
为laravel/vendor/laravel/framework/src/Illuminate/Session/Store.php
Lines 95 to 106 in ca57c28
这里的
read
方法,就是FileSessionHandler::read
laravel/vendor/laravel/framework/src/Illuminate/Session/FileSessionHandler.php
Lines 67 to 76 in ca57c28
执行到这里时, session 中的数据就被存放到了
Illuminate\Session\Store::$attributes
了。而执行
Session::get
时,laravel/vendor/laravel/framework/src/Illuminate/Session/Store.php
Lines 205 to 208 in ca57c28
正好就能从
Store::$attributes
中取出 session 的数据了。写入 session
当我们使用
Illuminate\Support\Facades\Session
时,操作是调用到的session
,也就是Illuminate\Session\SessionManager
。但是在Illuminate\Support\Manager
的__call
魔术方法,实现了将session.driver
载入到session
中被执行的能力。laravel/vendor/laravel/framework/src/Illuminate/Support/Manager.php
Lines 144 to 147 in ca57c28
假如我们执行
Session::put('key', 'value')
会穿透到
laravel/vendor/laravel/framework/src/Illuminate/Session/Store.php
Lines 265 to 274 in ca57c28
当我们手动
Session::save
时,或者程序终止运行触发register_shutdown_function
时,会触发laravel/vendor/laravel/framework/src/Illuminate/Session/Store.php
Lines 124 to 133 in ca57c28
也就是
FileSessionHandler::write
laravel/vendor/laravel/framework/src/Illuminate/Session/FileSessionHandler.php
Lines 81 to 87 in ca57c28
至此,写入 session 的流程走完。
The text was updated successfully, but these errors were encountered: