Configuring baseurl in cakephp

I am working on an application that runs on CakePHP and I use AJAX requests from the inside.

In all cases, for all ajax posts, I used url as

var ht = $.ajax({ type: "GET", url: "http://172.20.52.99/FormBuilder/index.php/forms/viewChoices/"+attribute_id, async: false }).responseText; var myObject = eval('(' + ht + ')'); 

Is there a way in CakePHP where I can specify my base URL as http://172.20.52.99/FormBuilder/index.php/ and call the base URL in all the places I want.

+4
source share
7 answers

Try writing the following code

 'http://'.$_SERVER['HTTP_HOST'].$this->base 

$_SERVER['HTTP_HOST'] ---- this will give you a working host

and $this->base --- will give you a working URL

+3
source

You can use $ this-> base to get the base url.

+2
source

you can use

 <?php echo Router::fullbaseUrl();?> 

.

See http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html for more details.

+2
source

You can set a default url for all your ajax requests this way:

 $.ajaxSetup({ url: 'http://172.20.52.99/FormBuilder/index.php/' }); 
+1
source

Use any following method

  • <?php echo $this->Html->url('/');?>

  • <?php Router::url('/', true); ?>

  • <?php echo $this->base;?>

  • <?php echo $this->webroot; ?>

  • Define the constant in Config / core.php as define("BASE_URL", "www.yoursite.com/"); and use BASE_URL anywhere in your project.

+1
source

I assume that I have the same scenario: for development, the site is available on localhost / cake, as a result of which the site is deployed in the root directory of example.com. In the header, I installed:

<base href="<?php echo $base_url ?>" /> ,

in AppContoller::beforeRender() I installed:

$this->set('base_url', 'http://'.$_SERVER['SERVER_NAME'].Router::url('/')); .

This works fine for everything except JS (thus AJAX) as it ignores base_url.

So I have a workaround (uses jQuery, but easy to replace without):

 forUrl = function(url) { return $('base').attr('href')+url.substr(1); } login = function() { $.post(forUrl('/ajax/profileDiv'), $('#profile-login-form').serialize(), function(data) { (...) }); } 
0
source

It may also be useful:

http://book.cakephp.org/view/1448/url

 <?php echo $this->Html->url('/posts', true); ?> //Output http://somedomain.com/posts 
0
source

Source: https://habr.com/ru/post/1311582/


All Articles