37

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('/'));
}

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

ajreal
  • 46,720
  • 11
  • 89
  • 119
Musa
  • 3,944
  • 4
  • 21
  • 27

7 Answers7

71

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.

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

Router::url('/', true);
RabidFire
  • 6,280
  • 1
  • 28
  • 24
  • As versions 2 and 3 are completely different and those who ask questions about cakephp + those who answer do not mention the version. I should mention that this worked for me on version 3.x . – Mehdi Nov 17 '19 at 04:15
29

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_URL anywhere in your project

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

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

Subodh Ghulaxe
  • 18,333
  • 14
  • 83
  • 102
11

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.

Keerthi Bandara
  • 159
  • 1
  • 7
10

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::url method is inherited by all Helpers), which is just a wrapper for the above Router method.

In either case, the second true parameter causes it to return the full URL.

deceze
  • 510,633
  • 85
  • 743
  • 889
2

You can use 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.

i.e.

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.

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

use Cake\Routing\Router
1

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; ?>.

Nigel
  • 1,695
  • 1
  • 13
  • 22
1

You can use FULL_BASE_URL constant.

Gr Brainstorm
  • 149
  • 2
  • 2