0

I have defined image_path in config.php and now need to access this variable in views like we use base_url().

Hos is it possible?

2 Answers 2

2

You would need to extend the url_helper. See the "Extending Helpers" section in the documentation.

In short, create a file name MY_url_helper.php in your application/helpers folder. (Assuming $config['subclass_prefix'] = 'MY_', in your config file.)

Add the following method.

if ( ! function_exists('image_path'))
{
    function image_path() 
    {
        $CI =& get_instance();
        return $CI->config->item('image_path');
    }
}

This should do the trick.

Sign up to request clarification or add additional context in comments.

2 Comments

I modified this bit of code to simply access it a config_item('NAME'); But why can we simply access base_url like base_url() but not the custom config variables?
I guess they favored readability and extensibility over functionality. After all, image_path() is cleaner than config_item('image_path'). It's more readable, easier to test and it follows the framework's principles. The first method allows you to add specific logic if needed in the future, while the config_item('NAME') makes it harder. I can see config_item('NAME') as a nice utility method of your helper, I wouldn't use it in the view.
2

Use constants.php, that's what it's for, this is out of my constants.php for image paths.

/*Constant paths*/
define('LOGO_PATH',APPPATH.'assets/images/manulogos/');
define('PROD_IMAGE_PATH',APPPATH.'../assets/images/prod_images/');

Then you just call the constant where you need it.

$imageName = $this->doUpload($control,PROD_IMAGE_PATH,$image,'all')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.