I'm using Yii2
and I have a situation where I want to make some code globally available, but only for that request.
My use case is based on this question where I am doing some permission checks via a matchCallback
.
I need to run a few different permission checks and if I can't do them all in the one place via the matchCallback
(because I can't access all of the data I need it seems at that specific time) - then I am trying to avoid having to run the query again.
So, I was wondering if I could store data retrieved from the database such as a JSON encoded permission array within my matchCallback
functionaltiy so I could then access that array within another action.
I know I could use flash data
, but that seems like it is more meant for the next
request. Is there anything available for just the current request or should I just use flash data
anyway?
I wonder what would be the best practice here too... As for me I'd use Yii::$app->params array for that purpose. It does not look very good as it's called 'params' and supposed to be some custom parameters like 'adminEmail' etc. But it works nice: it is readable and writable from everywhere and if you set some of its keys or values dynamically it will long only for this particular request - just what you want. It's like:
Yii::$app->params['myKey'] = $myValue;
...
$myValue = Yii::$app->params['myKey'];