Setting baseurl in cakephp

2020-07-18 05:26发布

I am working on an application which is running in CakePHP and I am using AJAX queries from within.

In all the cases for all the ajax post i have used the 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 any way in CakePHP where I can give my base URL as http://172.20.52.99/FormBuilder/index.php/ and to call the base URL in all the places I want.

标签: url cakephp
7条回答
狗以群分
2楼-- · 2020-07-18 05:48

you may use

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

as well.

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

查看更多
不美不萌又怎样
3楼-- · 2020-07-18 05:55

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

查看更多
Anthone
4楼-- · 2020-07-18 05:57

This might also be helpful:

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

<?php echo $this->Html->url('/posts', true); ?>

//Output
http://somedomain.com/posts
查看更多
▲ chillily
5楼-- · 2020-07-18 05:58

You could set default URL for all your ajax requests that way:

$.ajaxSetup({
  url: 'http://172.20.52.99/FormBuilder/index.php/'
});
查看更多
老娘就宠你
6楼-- · 2020-07-18 06:07

Try writing following code

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

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

and $this->base---will give you working url

查看更多
不美不萌又怎样
7楼-- · 2020-07-18 06:08

Use any one method of following

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

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

  3. <?php echo $this->base;?>

  4. <?php echo $this->webroot; ?>

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

查看更多
登录 后发表回答