Codeigniter: A fatal error occurs when using base_

2019-09-02 13:41发布

问题:

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

回答1:

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



回答2:

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