title: CakePHP - Handling urls in ajax calls
body: <p>What is the better way to deal with urls in ajax calls made in javascript files present in the webroot and because of that, are not interpreted by PHP?</p>
  
  <p>I'm using CakePHP and require.js and therefore would not put the javascript code directly in views. The only way I found was to declare a variable in the layout that receives the value of the webroot like this:</p>
  
  <p><code>&lt;script&gt;var webroot = "&lt;?php echo this-&gt;Html-&gt;url('/') ?&gt;" &lt;/script&gt;</code></p>
  
  <p>And then in my js files I hardcoded the urls to the ajax calls like this:</p>
  
  <p><code>$.getJSON(webroot + 'users/list', function(){ ... } );</code></p>
  
  <p>But it does not solve the problems if there are changes in the Routes file. I generally change the routes to be more friendly after I finished the project and this would cause a big problem if I have many ajax calls or urls been referenced  in js files.</p>