base_url in CakePHP(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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CakePHP 中的 base_url


基础教程推荐
- php中的PDF导出 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- php中的foreach复选框POST 2021-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01