What is the best way to to execute code before a page is loaded
Based on my previous question, in Enterprise Edition Magento it doesn't appear to be a good idea to use any of the available controller events if you are planning on having the full page caching enabled. It looks to be you only want to use these events if you are doing something with the actual page.
However, we have built some extensions for Magento that on controller_action_predispatch
, we have an observer and from there we grab a parameter in the URL and if it is set correctly we do some additional functionality from there. For example, we have made it so a client can put promo codes in urls for email campaigns and when they click that link it is attached to the customer's quote. We have to look for the parameter before the page is loaded and do our thing.
But now that there is this full page caching it isn't working correctly. So is it pr开发者_Go百科obably better to not do this with an Observer and just extend the code? Or are there better Observers to do this with. We tried to use HTTP send response before one and got mixed results.
If you cannot find a suitable event then request one, or create it yourself. Perhaps extending Enterprise_PageCache_Model_Processor
and checking to see what it returns, if non-empty then trigger the new event. Then watch both events with a single observer.
If you want to keep your investment in the controller_action_predispatch
events and stay in the Magento framework for your code, you could do a couple of things.
Add the URL param into the cache key. That way the FPC will serve the different versions of the page that the param can trigger while still giving the benefit of caching. Refer to this blog post for an example of creating your own PageCache container and overriding the cache key.
Exclude those controllers from FPC. Obviously this is only valid if the controllers affected are a subset of the overall site, otherwise you won't get any caching benefit.
Cheers, JD
Maybe auto_prepend_file directive could help?
精彩评论