php CakePHP 中的 base_url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4298552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:34:35  来源:igfitidea点击:

base_url in CakePHP

phpcakephp

提问by Musa

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

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

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

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

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

回答by RabidFire

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

就在这里。在您看来,您可以访问:

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

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

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

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

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

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

回答by Subodh Ghulaxe

Use anyone option below

使用下面的任何人选项

  1. <?php echo $this->Html->url('/');?>

  2. <?php Router::url('/', true); ?>

  3. <?php echo $this->base;?>

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

  5. Define constant in Config/core.php as define("BASE_URL", "www.yoursite.com/");and use BASE_URLanywhere in your project

  1. <?php echo $this->Html->url('/');?>

  2. <?php Router::url('/', true); ?>

  3. <?php echo $this->base;?>

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

  5. 在 Config/core.php 中定义常量 as define("BASE_URL", "www.yoursite.com/");BASE_URL在项目中的任何地方使用

and create a common helper with following functions

并创建一个具有以下功能的通用助手

<?php
class CommonHelper extends AppHelper {

    function get_url($url){
        return BASE_URL.$url;
    }

    function get_src($url){
        echo BASE_URL.$url;
    } 
}
?>

and use anywhere in project

并在项目中的任何地方使用

$this->redirect($this->Common->get_url("login");

<a href="<?php $this->Common->get_src('users/login');?>">login</a>

Don't forgot to include Common helper in controller

不要忘记在控制器中包含 Common helper

I recommend method 2 and 5 because they give complete url.

我推荐方法 2 和 5,因为它们提供了完整的 url。

回答by Keerthi Bandara

You may use

您可以使用

<?php echo Router::fullbaseUrl();?>

as well.

以及。

Refer http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.htmlfor more details.

有关更多详细信息,请参阅http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html

回答by deceze

Use Router::url('/', true)anywhere in your app.
Specifically in the View, you can use $this->Html->url('/', true)(or any other Helper in fact, the Helper::urlmethod is inherited by all Helpers), which is just a wrapper for the above Routermethod.

Router::url('/', true)在您的应用程序中的任何地方使用。
具体在View中,可以使用$this->Html->url('/', true)(或者其他任何Helper,其实Helper::url是所有Helper都继承了该方法),它只是对上述Router方法的封装。

In either case, the second trueparametercauses it to return the full URL.

无论哪种情况,第二个true参数都会使其返回完整的 URL。

回答by Nigel

For most purposes I'd suggest using the CakePHP HtmlHelper to generate URLs, that way you won't need to worry about the base URL. The most user friendly way of getting the base URL in your view, however, would be <?php echo $html->webroot; ?>.

对于大多数用途,我建议使用 CakePHP HtmlHelper 生成 URL,这样您就不必担心基本 URL。然而,在您的视图中获取基本 URL 的最用户友好的方式是 <?php echo $html->webroot; ?>.

回答by Antonis Charalambous

You can use Router::fullBaseUrl()

您可以使用 Router::fullBaseUrl()

If you have for example example.com/test and you want to ignore /test, you can use 'full' => false. Also if you want to force ssl you can add '_ssl' => true.

如果你有 example.com/test 并且你想忽略 /test,你可以使用 'full' => false。此外,如果您想强制 ssl,您可以添加 '_ssl' => true。

i.e.

IE

Router::fullBaseUrl(null, [ '_ssl' => true, 'full' => false]

Make sure you pass null as the first parameter as this is the base url in case you want to pass it.

确保将 null 作为第一个参数传递,因为这是基本 url,以防您想传递它。

note: you need to import Router so you can use above function:

注意:您需要导入路由器才能使用上述功能:

use Cake\Routing\Router

回答by Gr Brainstorm

You can use FULL_BASE_URLconstant.

您可以使用FULL_BASE_URL常量。