CakePHP 中的 base_url [英] base_url in CakePHP

查看:25
本文介绍了CakePHP 中的 base_url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数 Web 应用程序中,我们需要全局变量 base_url.在 cakephp 中获取 base_url 目前我将以下代码放在 app_controller.php 中的 beforeRender 方法

In most web applications we need global var base_url. In cakephp to get base_url currently i put the following code on beforeRender method in app_controller.php

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

还有什么办法吗?意味着是否有任何全局变量可用于获取基本 url 而不是这个?

Is there any alternative? Means is there any global variable available to get the base url rather than this?

推荐答案

是的,有.在您看来,您可以访问:

Yes, there is. In your view, you may access:

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

此外,您的主机信息存储在 $_SERVER['HTTP_HOST'] 变量中,以防万一.

Also, your host information is stored in the $_SERVER['HTTP_HOST'] variable in case you want that.

在您的控制器中,如果您想要完整的 URL,请使用:

In your controller, if you want full URLs, use this:

Router::url('/', true);

这篇关于CakePHP 中的 base_url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆