Currently I display images in the following way:
<img src="<?php echo base_url().USER_UPLOAD_URL.$post['userPhoto'] ?>" />
USER_UPLOAD_URL is defined inside application/config/constants.php.
define('USER_UPLOAD_URL', "uploads/user_uploads/");
Is there any way to include base_url() inside constants.php? In this way I wouldn't need to write each time base_url() inside view. Is there any alternative approach?
tnx
constants.php loading before config.php, so you can't use $config['base_url']
from constants.php.
But you can do something like that:
constants.php:
define('BASE_URL', "http://mysite.com");
define('USER_UPLOAD_URL', BASE_URL."uploads/user_uploads/");
config.php
$config['base_url'] = BASE_URL;
You can't use base_url()
in the constants file because the constants file is loaded first, and the base_url()
is only loaded when you either autoload the url helper, or load it per controller.
Here's a suggestion though, you could possibly define it in your controller:
public function __construct()
{
$this->load->helper('url');
define('USER_UPLOAD_URL', base_url('uploads/user_uploads/'));
}
Which would then be accessible to the view.
I'd complement the @Dale answer making use of a custom controller. At first place, create the controller at /application/core/GeneralController.php:
<?php
class GeneralController extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
define('USER_UPLOAD_URL', base_url('uploads/user_uploads/'));
}
}
Now, you can extends all controllers from GeneralController
instead of CI_Controller, because USER_UPLOAD_URL
is now defined
It is an old question still i overcame this issue using APPPATH
.
APPPATH
works inside constants.php. but it returns path as var/www/html/projectfolder/application
inside application folder you can store you data within a new folder or third_party
folder.
In constants you can define as eg define('pace-min-js',APPPATH.'third_party/luna/vendor/pacejs/pace.min.js');