Search code examples
phpcodeignitercodeigniter-3codeigniter-2

In codeigniter, how to make assets folder work by putting it in the application folder?


I want to put my assets folder inside the application folder of codeigniter just like:

application > assets > css > style.css 

and I want to give assets files links in my view files.


Solution

  • The best practice in codeigniter for set up the assets as below flow:

    • application
    • assets
    • system

    But you want:

    • application > assets > css > style.css
    • system

    but if you want to keep the assets folder inside the application, then you've to do some extra functionality as below: First, make sure that you've loaded the URL helper in your controller which is:

    $this->load->helper("url");
    

    you have to first create a helper named "my_helper.php" at "application/helpers" directory with this code:

    if ( ! function_exists('asset_url()'))
    {
      function asset_url() {
    
        return base_url().'application/assets/';
    
      }
    }
    

    Now, you have to load this helper into your controller as below:

    $this->load->helper("my_helper");
    

    Now replace your .htaccess code at 'application/'' directory with the below code:

    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|assets|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]
    

    After doing all the above functionality, you have to declare your assets in view as below:

    <link rel="stylesheet" href="<?php echo asset_url(); ?>css/style.css">