codeigniter base_url inside constants file codeigniter base_url inside constants file codeigniter codeigniter

codeigniter base_url inside constants file


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:

<?phpclass 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