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?
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.
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.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')