php – 在Laravel 5.1中更改会话文件名
所以当有人访问/ storage / framework / sessions文件夹中的网站时,Laravel会保存自己的会话文件.每个会话文件的名称都是随机生成的字母数字唯一名称.但是,我想以某种方式重命名文件并给出自己的自定义名称.我有两个选择.
>创建会话文件(通过创建,复制,替换)手动更改文件名称 我的主要目标是将每个用户的会话文件重命名为存储在我的数据库中的自己的userid.所以这些名字仍然是唯一的,唯一的区别是,我可以搜索文件比如果他们有随机的字母数字名称更容易. 所以如果有人知道我可以做任何以上的方法,或者如果你能想到一个更好的方式来实现这一点,那将是巨大的.任何帮助是极大的赞赏! 编辑:决定在这里更新我决定做的最后.我决定不使用Laravel生成的内置会话文件,并意识到自己创建文件要容易得多,只需让每个客户端访问它.谢谢大家!
您应该将会话扩展代码放在AppServiceProvider的引导方法中. 实现SessionHandlerInterface 应用程序/提供者/ AppServiceProvider.php <?php namespace AppProviders; use Session; use IlluminateSupportServiceProvider; use AppHandlersMyFileHandler; class AppServiceProvider extends ServiceProvider { public function boot() { Session::extend('file',function($app) { return new MyFileHandler(); }); } } 请注意,我们的自定义会话驱动程序应该实现SessionHandlerInterface.该界面只包含一些我们需要实现的简单方法. 应用程序/处理程序/ MyFileHandler.php <?php namespace AppHandlers; use SessionHandlerInterface; class MyFileHandler implements SessionHandlerInterface { public function open($savePath,$sessionName) {} public function close() {} public function read($sessionId) {} public function write($sessionId,$data) {} public function destroy($sessionId) {} public function gc($lifetime) {} } 或者您可以从FileSessionHandler扩展MyFileHandler并覆盖相关方法. 扩展FileSessionHandler 应用程序/提供者/ AppServiceProvider.php <?php namespace AppProviders; use Session; use IlluminateSupportServiceProvider; use IlluminateSessionFileSessionHandler; use AppHandlersMyFileHandler; class AppServiceProvider extends ServiceProvider { public function boot() { Session::extend('file',function($app) { $path = $app['config']['session.files']; return new MyFileHandler($app['files'],$path); }); } } 应用程序/处理程序/ MyFileHandler.php <?php namespace AppHandlers; use IlluminateFilesystemFilesystem; use IlluminateSessionFileSessionHandler; class MyFileHandler extends FileSessionHandler { public function __construct(Filesystem $files,$path) { parent::__construct($files,$path); } } 您可以在“扩展框架”文档的“会话”部分找到更多内容. https://laravel.com/docs/5.0/extending#session (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |