Posted on 14/9/08 by Felix Geisendörfer
this is post #25 of my 30 day challenge.
This is a really annoying issue, especially since we are all used to CakePHP fixing it for us when using Router::url. So how could we create our own url() function in JavaSccript that will solve this problem for us?
Well the solution is simple. All we need to do is to store the information on the base url somewhere in the html. Since we are lucky, the html specification already knows an element for this called <base>.
So when we open up our app/views/layouts.ctp file, all we need to put into the <head> section is the following:
Now whenever we want to make an ajax call, we can simply do:
Of course another way of achieving the same result is to require your site to run within its own domain (virtual host). However, I like building apps that can also run from within a sub folder as it makes them more portable and its easier for other people to set the site up and get involved in the project.
Alright, looking forward to alternative approaches & feedback.
-- Felix Geisendörfer aka the_undefined
You can skip to the end and add a comment.
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.