javascriptphpbackbone.jsslim

Is there any way to log the url and hash fragment in a single page application


I'm developing a single page application with a PHP backend using Slim Framework and a JavaScript frontend client using Backbone.js and came across a situation where I want to log requests that result in a 404 error but knowing that hash fragments are not recorded in the request on the backend. I'm wondering if there is a work-around.

My first thoughts was to have JavaScript write a cookie with the hash fragment and have PHP read that for logging.

For logging I'm using a custom Monolog handler for Doctrine 2.


Solution

  • This is what I use for sending those fragments off to Google Analytics or off to my error reporting system:

        var url = Backbone.history.getFragment();
    
        if (!/^\//.test(url)) {
            url = "/" + url;    
        }