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 - if 语句中的倒序 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01