Vivek Mandal
Vivek Mandal

Reputation: 43

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.

Upvotes: 4

Views: 809

Answers (2)

Towsif
Towsif

Reputation: 320

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">

Upvotes: 3

  1. Set up library: application folder->config folder->autoload.php
  2. In autoload.php find $autoload['helper'] = array('url'); and add 'url' in it.
  3. 1st create folder assets in application. 2nd create folder CSS in assets and put the .css file in it.
  4. Called the CSS file with this style < link rel="stylesheet" type="text/css" href="assets/css/yourFileName.css" >

Upvotes: -1

Related Questions