加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – 在Laravel 5.1中更改会话文件名

发布时间:2020-12-14 19:41:47 所属栏目:大数据 来源:网络整理
导读:所以当有人访问/ storage / framework / sessions文件夹中的网站时,Laravel会保存自己的会话文件.每个会话文件的名称都是随机生成的字母数字唯一名称.但是,我想以某种方式重命名文件并给出自己的自定义名称.我有两个选择. 创建会话文件(通过创建,复制,替换)
所以当有人访问/ storage / framework / sessions文件夹中的网站时,Laravel会保存自己的会话文件.每个会话文件的名称都是随机生成的字母数字唯一名称.但是,我想以某种方式重命名文件并给出自己的自定义名称.我有两个选择.

>创建会话文件(通过创建,复制,替换)手动更改文件名称
>查找随机生成字母数字名称的函数,并用我自己的方式为每个文件设置唯一的名称进行更改(此方法可能会减少并发症)

我的主要目标是将每个用户的会话文件重命名为存储在我的数据库中的自己的userid.所以这些名字仍然是唯一的,唯一的区别是,我可以搜索文件比如果他们有随机的字母数字名称更容易.

所以如果有人知道我可以做任何以上的方法,或者如果你能想到一个更好的方式来实现这一点,那将是巨大的.任何帮助是极大的赞赏!

编辑:决定在这里更新我决定做的最后.我决定不使用Laravel生成的内置会话文件,并意识到自己创建文件要容易得多,只需让每个客户端访问它.谢谢大家!

Laravel has several Manager classes that manage the creation of
driver-based components. These include the cache,session,
authentication,and queue components. The manager class is responsible
for creating a particular driver implementation based on the
application’s configuration. For example,the SessionManager class can
create File,Database,Cookie and various other implementations of
session drivers.

Each of these managers includes an extend method which may be used to
easily inject new driver resolution functionality into the manager.

To extending Laravel with a custom session driver,we will use the
extend method to register our custom code:

您应该将会话扩展代码放在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读