codeigniter base_url inside constants file

帅比萌擦擦* 提交于 2019-12-19 09:24:43

问题


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


回答1:


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;



回答2:


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.




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/13823530/codeigniter-base-url-inside-constants-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!