CakePHP 中的 base_url

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Codeigniter htaccess to remove index.php and www(Codeigniter htaccess 删除 index.php 和 www)
htaccess mod_rewrite part of url to GET variable(htaccess mod_rewrite url 的一部分到 GET 变量)
Replacing a querystring parameter value using mod_rewrite(使用 mod_rewrite 替换查询字符串参数值)
.htaccess in subdirectory #39;overriding#39; parent htaccess(子目录“覆盖父 htaccess 中的 .htaccess)
How to rewrite SEO friendly url#39;s like stackoverflow(如何像stackoverflow一样重写SEO友好的url)
Is it okay to have a very long .htaccess file?(有一个很长的 .htaccess 文件可以吗?)