在cakephp中设置baseurl

时间:2010-06-02 04:58:13

标签: url cakephp

我正在开发一个在CakePHP中运行的应用程序,我正在使用内部的AJAX查询。

在所有ajax帖子的所有情况下,我都使用了网址

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 + ')');

CakePHP中有什么办法可以将我的基本网址设为http://172.20.52.99/FormBuilder/index.php/,并在我想要的所有地方调用基本网址。

7 个答案:

答案 0 :(得分:3)

尝试编写以下代码

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

$_SERVER['HTTP_HOST'] ----它会给你工作主持人

$this->base ---会给你工作的网址

答案 1 :(得分:2)

您可以使用$ this-> base来获取基本网址。

答案 2 :(得分:2)

你可以使用

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

有关详细信息,请参阅http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html

答案 3 :(得分:1)

您可以通过以下方式设置所有ajax请求的默认网址:

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

答案 4 :(得分:1)

使用以下任何一种方法

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

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

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

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

  5. 将Config / core.php中的常量定义为define("BASE_URL", "www.yoursite.com/");,并在项目的任何位置使用BASE_URL

答案 5 :(得分:0)

我假设我的情况相同:对于开发,该站点在localhost / cake上可用,在生产中,该站点部署在example.com的根目录中。在我设置的标题中:

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

AppContoller::beforeRender()我设置:

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

这适用于所有内容,除了JS(因此是AJAX),因为它忽略了base_url。

因此我有一个解决方法(使用jQuery,但很容易替换):

forUrl = function(url) {
    return $('base').attr('href')+url.substr(1);
}

login = function() {
    $.post(forUrl('/ajax/profileDiv'), $('#profile-login-form').serialize(),
        function(data) {
            (...)
        });
}

答案 6 :(得分:0)

这可能也有帮助:

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

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

//Output
http://somedomain.com/posts
相关问题