Save and repeat a request

What I’m trying to accomplish is the ability to save and repeat arbitrary requests (possibly containing POST data and other nastiness). I’d like this to happen at as high a level as is feasible.

My first thought was to serialize $_SERVER, $_GET, $_POST, etc as well as Yii::app()->request and persist them using something like CWebUser->setState(). In terms of repeating the request, I was thinking of just restoring the superglobals and the request component, then calling Yii::app()->processRequest(). I’d need a bit of extra magic if I want the user to see the correct URL, but I can probably just do a redirect upfront. A problem that I can foresee with this approach is that there doesn’t seem to be an easy way to set the request component (there’s no Yii::app()->setRequest()).

A lower-level approach I’m also considering is to record the relevant request information (again, mostly just the superglobals) then making the client handle it with some clever JavaScript (something like turning the request into a <form>-based representation and then programmatically submitting it). Though this will almost certainly work, it seems hacky and requires the user to have JavaScript enabled.

My use case is a paywall. Some user actions require upgrading to a paid account, and what we want is for the user to be able to attempt the action and be prompted to upgrade, and for the original action to be performed automatically after they complete the upgrade.

Has anyone else ever tried anything like this? Do you have any advice? I’ve dug through the documentation and can’t find much that would help me out, but am I missing anything? Am I crazy for even attempting this?

It all depends of the implementation…

basically you can only pass one parameter that can take a name of the original action…and upon upgrade just check that name and redirect to proper URL…

Since you’ve asked: Yes, your approach looks a little odd to me. Why don’t you for example use models as container for your data, as it’s default in Yii? You then can add utility methods to save the model attributes somewhere and load them back.

Your approach might work - somehow. But you’re far of the standard path of Yii. And i don’t see the necessity why you would want to leave this path.

This is what I have implemented right now, but the problem is that the original requests often contain POST data and such. I could do something like encrypt all of the non-URL request data, put it in a GET parameter, then decrypt and restore when I want to "replay" the original request, but that seems even hackier than my JavaScript idea.

Do you mean serialize the request data as a model and use that to persist it? If so, that’s not a bad idea, but it doesn’t solve the problem of how I actually repeat stored requests. If not, could you explain a bit more?

Why would you still want to repeat the request? You can process the data in GET/POST like you would, if the user was already upgraded. If he wasn’t, you save these validated model attributes e.g. in the session and redirect the user to some sign up page. After that you can come back and restore the valid model attributes from there. No need to complicate things.