简体   繁体   English

CakePHP中的base_url

[英]base_url in CakePHP

In most web applications we need global var base_url. 在大多数Web应用程序中,我们需要全局var base_url。 In cakephp to get base_url currently i put the following code on beforeRender method in app_controller.php 在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而不是这个?

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);

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/"); 在Config / core.php中定义常量作为define("BASE_URL", "www.yoursite.com/"); and use BASE_URL anywhere in your project 并在项目的任何位置使用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。

You may use 你可以用

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

as well. 同样。

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

Use Router::url('/', true) anywhere in your app. 在您的应用中的任何位置使用Router::url('/', true)
Specifically in the View, you can use $this->Html->url('/', true) (or any other Helper in fact, the Helper::url method is inherited by all Helpers), which is just a wrapper for the above Router method. 特别是在View中,你可以使用$this->Html->url('/', true) (或者其他任何Helper实际上,所有Helper都继承了Helper::url方法),这只是一个包装器上面的Router方法。

In either case, the second true parameter causes it to return the full URL. 在任何一种情况下, 第二个true参数都会使它返回完整的URL。

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. 在大多数情况下,我建议使用CakePHP HtmlHelper生成URL,这样您就不必担心基本URL了。 The most user friendly way of getting the base URL in your view, however, would be <?php echo $html->webroot; ?> 但是,在视图中获取基本URL的最友好的方式是<?php echo $html->webroot; ?> <?php echo $html->webroot; ?> . <?php echo $html->webroot; ?>

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. 如果您有例如example.com/test并且想要忽略/ test,则可以使用'full'=> false。 Also if you want to force ssl you can add '_ssl' => true. 此外,如果你想强制ssl你可以添加'_ssl'=> true。

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

您可以使用FULL_BASE_URL常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM