douduan5753 2014-06-18 08:34
浏览 38
已采纳

创建我们自己的会话处理程序

I'm trying to create my own session handler. I've found this resource. The problem with that, is that he make changes to vendor directory which is something I don't want to.

The reason for that is, I'm working with others, it's a collaborative project using version control system (pushing the main application ignore the vendor folder ) and besides that I presume with each composer install all changes will be lost (although I'm not sure).

It crossed my mind to change the DatabaseSessionHandler, after all , all I wanted was to change the field names of the table that Laravel uses for storing the session (I'm working on a pre-existing database), but it would be the same as I mentioned above.

Is there any way I could create my own session handler and use it in my application? A service provider or something even better?

Links will be appreciated.

Update

Something I want to clarify, I want to be able to use Laravels API.

Another resource I've found is Laravels doc how to write a session extension, but I think a lot of stuff is missing. It says I have to create a service provider cause sessions are started very early in the request-lifecycle. Any directions from there?

  • 写回答

3条回答 默认 最新

  • dspvin19712 2014-06-18 10:06
    关注

    It says I have to create a service provider cause sessions are started very early in the request-lifecycle. Any directions from there?

    What that actually means is that you have to register a Session Service Provider in the IoC-Container very early in the request-lifecycle.

    Since the bindings in your app/config/app.php will be registered very early during the bootstrapping process of laravel, it is the perfect place to bind your custom SessionHandler-Extension.

    Basically you need the following things to tackle your problem:

    • A Service Provider that binds a new instance of a SessionHandlerInterface (your concrete Custom Session Handler) to the IoC Container
    • And edit in your app/config/app.php that adds your new Service Provider
    • Your Custom Session Handler Class

    Let's get started:

    Bootstrapping the Service Provider:

    The Docs tell us to add our Custom Service Provider below the Illuminate\Session\SessionServiceProvider so in your app/config/app.php add the following line below laravel SessionServiceProvider:

    'MyProject\Extension\CustomSessionServiceProvider',

    Now during laravels bootstrapping process out CustomSessionServiceProvider will be loaded right after laravels. In our custom provider we will do the actual binding.

    Creating the Service Provider:

    Now that you made sure the Service Provider is being loaded we will implement it. Within the service provider we can overwrite the binding of laravels DatabaseSessionHandler which we will do.

    <?php namespace MyProject\Extension;
    
    use Illuminate\Support\ServiceProvider;
    use Session;
    
    class CustomSessionServiceProvider extends ServiceProvider {
    
        public function register()
        {
            $connection = $this->app['config']['session.connection'];
            $table = $this->app['config']['session.table'];
    
            $this->app['session']->extend('database', function($app) use ($connection, $table){
                return new \MyProject\Extension\CustomDatabaseSessionHandler(
                    $this->app['db']->connection($connection),
                    $table
                );
            });
        }
    
    }
    

    First we grab the connection type that we use to store our session and then the table where the sessions will be stored.

    Since you only want to change column names we don't need to implement a whole new Database Session Handler. Instead let's extend Laravels Illuminate\Session\DatabaseSessionHandler and overwrite the necessary methods.

    Laravels DatabaseSessionHandler has two dependencies. An implementation of the ConnectionInterface and a table name. Both are injected into our CustomDatabaseSessionHandler which you can see above.

    Then we just return our CustomDatabaseSessionHandler in the closure.

    Creating the actual CustomDatabaseSessionHandler

    Now that we're ready to fire off the new CustomDatabaseSessionHandler let's create it.

    There is not much to do. Only four methods use the hardcoded columns. We will just extend the Illuminate\Session\DatabaseSessionHandler class and overwrite those four.

    <?php namespace MyProject\Extension;
    
    use Illuminate\Session\DatabaseSessionHandler;
    
    class CustomDatabaseSessionHandler extends DatabaseSessionHandler {
    
        public function read($sessionId)
        {
            // Reading the session
        }
    
        public function write($sessionId, $data)
        {
            // Writing the session
        }
    
        public function destroy($sessionId)
        {
            // Destryoing the Session
        }
    
        public function gc($lifetime)
        {
            // Cleaning up expired sessions
        }
    
    }
    

    Since you want to only change column names you can even copy the method bodies from the parent class and just change what you want.

    That's it. Happy coding and have fun with Laravel!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用