base_url in CakePHP

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.


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.


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

Tags:

Php

Cakephp