4

We need to do custom logic at Session::get like for example, for some reason, if it gets a null value do something else before returning null to the caller..

We had a look at extending the session here but the handler that we need to override is at vendor/laravel/framework/src/Illuminate/Session/Store.php with method get

So, what is the recommended way to achieve the overriding of method get at vendor/laravel/framework/src/Illuminate/Session/Store.php ?

Mohammad AL-Raoosh
  • 721
  • 1
  • 12
  • 32

2 Answers2

2

You can create your own SessionServiceProvider, extending Illuminate\Support\ServiceProvider\SessionServiceProvider and override registerSessionDriver() in order to return a custom instance of the store class.

Then comment the Illuminate\Support\ServiceProvider\SessionServiceProvider in config/app.php and add your custom SessionServiceProvider.

A working example: https://gist.github.com/thiagorb/d4f4afaafa23a7a564b5675db952fbb2

Thiago Barcala
  • 6,463
  • 2
  • 20
  • 23
  • @AlexeyMezenin, you don't need to reimplement them, you can just extend them. – Thiago Barcala Jan 02 '18 at 08:28
  • You can't just extend one method this way. You still need to override a lot of stuff (and then maintain the code). It's a huge overkill for a small extension. Also, I'm pretty sure that the most of the developers couldn't even do that because it's a complex task and not just some method overriding. – Alexey Mezenin Jan 02 '18 at 08:31
  • I don't agree with your opinion. These classes are supposed to be easy to extend. That's the whole purpose of having service providers. You should be able to replace the implementation easily. – Thiago Barcala Jan 02 '18 at 08:42
  • @AlexeyMezenin, I added a gist with an working implementation. Note that I implemented even the encrypted store, so this solution is very generic. – Thiago Barcala Jan 02 '18 at 17:13
  • Thanks. I didn't test it, but I suppose you did. So you've created 4 classes and edited `config/app.php`. Now your team should worry about maintaining these 4 classes (thank god it's just 4 in this case). Would you really do this in a real project instead of creating or overriding a global helper or some method in `Helpers` class? Upvote for the effort anyway. – Alexey Mezenin Jan 02 '18 at 18:22
  • I can imagine situations where I would. Depending on how big the project already is. I don’t think these 4 classes add much complexity. Service providers are used often in the framework, so I would say they are like configuration. Besides that, depending on how the post author want the system to behave, it might be a good option. You need to take into account also that this solution is absolutely generic, without any other assumption about the rest of the system. It would work with existing modules and so on. So in the end it is just a matter of choosing between trade offs. – Thiago Barcala Jan 02 '18 at 21:55
1

You could create your own session() like helper:

function sessionCheck($value)
{
    if (is_null(session($value))) {
        // Do something
    }

    return session($value);
}

Or override the existing one.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279