Search code examples
cakephpcakephp-2.6

How to get base url inside controller in CakePHP


I am using CakePHP 2.6.7. I want to get the following url:

www.mydomain.com/OrderFromReseller/82BC1562-22F9-4326-8B4B-370129710E8C

I try as:

$resellerURL = $this->webroot . 'OrderFromReseller/' . $this->request->data['Reseller']['api_key']; 

But when I echo the value of $resellerURL it echos http://orderfromreseller/82BC1562-22F9-4326-8B4B-370129710E8C.

Is there any alternative of $this->webroot to get base url properly inside cakephp?


Solution

  • You want to use Router::url() to get the full URL. For example to get the full web address for the homepage:-

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

    You can pass any router array as the first parameter to get the full URL as long as you pass true as the second parameter. For example:-

    $url = Router::url(
        ['controller' => 'pages', 'action' => 'display', 'test'],
        true
    );