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
You can't use
base_url()
in the constants file because the constants file is loaded first, and thebase_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:
Which would then be accessible to the view.
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:
config.php
I'd complement the @Dale answer making use of a custom controller. At first place, create the controller at /application/core/GeneralController.php:
Now, you can extends all controllers from
GeneralController
instead of CI_Controller, becauseUSER_UPLOAD_URL
is now definedIt is an old question still i overcame this issue using
APPPATH
.APPPATH
works inside constants.php. but it returns path asvar/www/html/projectfolder/application
inside application folder you can store you data within a new folder orthird_party
folder. In constants you can define as egdefine('pace-min-js',APPPATH.'third_party/luna/vendor/pacejs/pace.min.js');