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

[SOLVED]Using Phalcon to create a project using HTML5 History

Hi, I was searching the web for a framework to build my new project and found Phalcon which seems to be really interesting. I started to read its documentation, trying samples and build little applications to learn how it works.

But, there is one more thing I'd like to do before using Phalcon. I need HTML5 history to work on my application. This means that I'd like to show HTML or JSON depending on http headers. And I'd like to avoid creating the 3 same lines of code in every action.

For example : I go to : https://domain.com/search/query/this+is+a+test -> Call SearchController::queryAction() and send HTML response I go to : https://domain.com/search/index, fill the form query and click "Search", jquery ajax call https://domain.com/search/query/this+is+a+test using ajax http header and get the response which would contain for example :

{
    "title" : "this is a test",
    "body" : "<div>[...]</div>"
    "url" : "https://domain.com/search/index"
}

And use pushstate.

The problem is that I need this kind of double response type for almost all my controller (if not all of them).

Thanks, cylessia



70
Accepted
answer

Hi! I solved this problem in my project. On event 'afterExecuteRoute' in base controller I check ajax headers on request. After I attach handler on 'view::afterRender' event, where rendered content wrapped by json. See code here: https://gist.github.com/raplos/6560810

Good luck!



5.1k

This is exactly what I needed, thanks ! I figured out another way by implementing a manual bootstrap but it became to be messy and complicated.