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


基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01