5

I am using ajax with jQuery in my cakePHP application.
and my javascript function is placed inside a javascript file.

now in my local system the files are kept in "/sample" directory so the the path while i call the function will be

in ajax.js

$.post({url : "/sample/controller/action"}) 

but after hosting it the url will become

$.post({url : "/mydomain.com/controller/action"}) 

in cakePHP we $html->url to generate urls
but since this code is in js file i can't use that function

i don't want to change the all ajax action urls manually before hosting

5 Answers 5

23

What to do is in your master template for your cake app create a global javascript variable that can be used throughout your application. Make sure it exists befor you do any JS includes too.

<head> ... <script type="text/javascript">var myBaseUrl = '<?php echo $html->url; ?>';</script> ... <script type="text/javascript" src="mycustomJSfile.js"> ... </head> 

Now you can do things like this from any view file you have in your MVC framework app.

$.post({url: myBaseUrl + 'controller/action'}); 
Sign up to request clarification or add additional context in comments.

4 Comments

I do this for every web application i write using any framework. It's even more handy if you're using Smarty and need the baseUrl in javascript. You'd have to do {/literal}{$baseUrl}{/literal} which is a royal PITA. The above solution solves this nicely.
This is the correct answer but it didn't work for me. It may be because I'm using Cake 2 and this solution was for an earlier version, though. Nonetheless, the code I had to use to get it to work in Cake2 was <script type="text/javascript">var myBaseUrl = '<?php echo $this->Html->url('/'); ?>';</script>
And for CakePHP 3, I used var appBaseUrl = '<?php echo $this->Url->build('/', true); ?>';
How do I pass parameters to the controller function?
6

I'm updating Paul Dragoonis's answer to reflect the latest CakePHP version (2.2).

In your layout file, set the JavaScript variable with CakePHP's JSHelper: <?php echo $this->Js->set('url', $this->request->base); ?>, where $this->request is an instance of CakeRequest and gives information on the current request.

After the line above, write the buffer with <?php echo $this->Js->writeBuffer(); ?>.

Then, you can access this variable in JavaScript with app.url.

Comments

3

@irtiza your answer is appreciated but for cakephp latest version 3.x this will work otherwise you will get routing error.

ulr:'<?php echo \Cake\Routing\Router::url(array('controller' => 'controllername', 'action' => 'actionname')); ?>' 

Comments

1

in reply to Paul Dragoonis you can use $this->webroot if you use subfolders.

Comments

1

use

echo Router::url(array('controller' => 'Users', 'action' => 'all')); 

Will output;

/Users/all 

in js

$.post({url : "<?php echo Router::url(array('controller' => 'Users', 'action' => 'all')); ?>"}) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.