Amr
Amr

Reputation: 5159

Codeigniter: A fatal error occurs when using base_url() function to define a constant in "application/config/constants.php" file

I'm using Codeigniter and right now I'm trying to define some constants to hold full paths to some folders like css, js, images, etc. and I've tried to define these constants in application/config/constants.php file like this:

define("PATH_TO_CSS_FOLDER", base_url("assets/css"));

but I've got this error:

Fatal error: Call to undefined function base_url() in D:\xampp\htdocs\demo\application\config\constants.php on line 44

so how can I define these constants without having to write the absolute path in the constants.php file like this:

define("PATH_TO_CSS_FOLDER", "http://my-website.com/assets/css");

Upvotes: 0

Views: 1409

Answers (2)

Rajeev Ranjan
Rajeev Ranjan

Reputation: 4142

you can define rest of path in constant.php

define("PATH_TO_CSS_FOLDER","assets/css");

use it later in ohter places

<?php echo base_url(PATH_TO_CSS_FOLDER);?>

you might be knowing that constants loads first then any helper

Upvotes: 3

user2486495
user2486495

Reputation: 1729

You have to load the url helper to access that function. Either you add

$this->load->helper('url');

somewhere in your controller, or you can put it into the application/config/autoload.php to be loaded automatically everywhere.

you have to use echo before base_url() function. otherwise it woudn't print the base url. like

echo base_url();

Upvotes: 0

Related Questions