We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Which event is best for accomplishing my goal?

I am implementing a demo project using Phalcon and jQuery PJAX.

Currently, I am using the beforeExecuteRoute on a base controller to disable the layouts on an ajax response, allowing just the view to be returned. So I am checking if the request is an ajax/pjax response, and if it is, I am disabling the layouts.

I want to disable the layouts at the last minute before the view begins being processed, to allow individual controllers to override the fact that it is pjax and force a hard refresh.

What event would be the best option for updating this before the view is sent? afterDispatch? afterDispatchLoop? I am unsure of the difference between those two. Maybe afterExecuteRoute?

I would appreciate any guidance and I'm happy to clarify if needed.

Thank you



39.4k
Accepted
answer

Use the Events Manager to process things in the beforeRender event of the view.

https://docs.phalcon.io/en/latest/reference/views.html#view-events



12.6k
edited Nov '14

Use the Events Manager to process things in the beforeRender event of the view.

https://docs.phalcon.io/en/latest/reference/views.html#view-events

Events manager ended up working pretty well.

However, I was shown a better way for this specific scenario in this post.

Turns out afterExecuteRoute is a good choice for executing code before the view is rendered and it's a bit cleaner for something as simple as what I was trying to accomplish.

Thanks!