1

I was wondering if there's any way to do something similar to what HtmlHelper does in Cake views: instead of having to write the URL manually, calling some kind of helper to make it for you.

I have taken a look at this post but it is from 2010 and maybe there's something new now...

Currently I have codes like this at my javascript files:

window.location.href = 'http://' + document.domain +'/cakephp/posts/view/'+$(this).attr('data-id'); 

But if i change the cakephp folder name, or I use another configuration on the server or something similar, the URL changes and I should change manually all the codes with this type of URL.

I wonder if there's something similar to:

echo $this->Html->link('controller' => 'users' , 'action' => 'login'); 

1 Answer 1

0

Are you referring to inline or external JavaScript? Inline is rather easy using the url method, which accepts the same arguments as link (an array or string containing the URL).

Using it in external files is a bit trickier. I define a JS variable containing the site path before loading the JS files.

<script type="text/javascript"> //<![CDATA[ var SITE_URL = "http://www.example.com/cakeapp/"; //]]> </script> 

In my external JavaScript files I can then reference SITE_URL when I need it. When you change domains or rename the cakeapp directory you only have to alter the SITE_URL variable.

Sign up to request clarification or add additional context in comments.

2 Comments

Im talking about external js files. Your solution is quite similar to the one given in the link i posted, i was wonderting if there's anything better to work with inside the JS files.
Ah you're right, my solution is identical to the one in the post you referred to. I don't know of any other method, but the above is still being used in recent versions of Wordpress for setting up AJAX calls for example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.