Category: Routing

Q: Get base_url in controller

I need to fix one of my client's cakephp application (version 2.4). And in most web applications we need base_url to get current path for link building. Now I want to build a link to image so:

$base_url = 'http://' . $_SERVER['SERVER_NAME'] . Router::url('/');

$image_path = $base_url . '/img/abcd.png';

I wonder if there is any alternative? I'm sure there should be a global configuration. 

SOLVED
2 Answers

Version: 2.4

user
Pidara

4

Accepted by Pidara

To get full url in controller:

$base_url = Router::url('/', true);
$image_path = $base_url . '/img/abcd.png';
Kristi
Kristi

137

use App\Controller\Router;

$image_path = Router::url('/', true) . '/img/abcd.png';
Pidara
Pidara

4

Created: 26 Sep '16

Last Reply: 26 Sep '16

Replies: 2

Views: 447

Votes: 0

Welcome to Aero Coding!

Aero Coding is a CakePHP-focused Q&A community for professional and enthusiast cake bakers. It's built and run by you as part of the community.


Join Now Tour

Download Cakephp

Start baking your own CakePHP application!


Cakephp All Versions