CodeIgniter Forums
Best practice for handling assets (images/css etc) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Best practice for handling assets (images/css etc) (/showthread.php?tid=66309)



Best practice for handling assets (images/css etc) - krishg - 10-06-2016

Dear CI community!

Our assets folder is at the same level as the application and system folder.

So, we have created a rule in .htaccess to handle URIs starting with /assets

Is there a best practice to handling assets within the CI framework?

If we want to continue to keep assets outside CI, but don't want to do .htaccess based routing, is there a way to set up routes in CI for this scenario.

Thanks in advance
Krish


RE: Best practice for handling assets (images/css etc) - InsiteFX - 10-06-2016

You can create your helper to point to your assets folder, my is always in the route of my server but this is how you can do it using a asset() method

./application/helpers/asset_helper.php

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');


/**
 * asset ()
 * ------------------------------------------------------------------------
 *
 * Assumes that all resources are in a asset directory in the root
 * along with index.php.
 *
 * USAGE:
 *
 * asset('path/filename.ext')
 */
if ( ! function_exists('asset'))
{
    
/**
     * method ()
     * ---------------------------------------------------------------------------
     *
     * @param   $uri
     * @return  mixed
     */
    
function asset($uri)
    {
        
$_ci =& get_instance();

        return 
$_ci->config->base_url('assets/'.$uri);
    }


You will have to play around with this but you should be able to make it work.


RE: Best practice for handling assets (images/css etc) - Narf - 10-07-2016

Just put them inside your webroot, or at symlink there ... anything else is extra work for nothing.