Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

php - Codeigniter assets folder best practice

I'm fairly new to codeigniter, But i'm learning well, I'm going to add a css, images, js, ... folder but I'm not sure where to put it

Someone told me to make a 'public' folder

system
application
public
    css
    images

And then in your index.php ( in the public folder ) adjust accordingly

$system_path = '../system'; $application_path = '../application';

But when I do that i get a 404 ( not the ci 404, but a really-not-found one )

Anyone has any idea what I might be doing wrong? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have this setup:

application
assets
system
.htaccess

and in the "assets" folder i have subfolders like "img" or "js" and so on. I also use "utility helper" to help me point to that folder.

If you want to try it, you have to first create a helper named "utility_helper.php" with this code:

     <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

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

and store it in

     application/helpers/

then you have to autoload that helper, you go to:

  application/config/autoload.php

and auto load the helper (example: )

  $autoload['helper'] = array('form', 'url', 'utility');

you have also to route to that folder ('application/config/routes.php')

       $route['assets/(:any)'] = 'assets/$1';

and have a .htaccess file with this content:

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

now you can simply include external scripts, css example:

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

where css is the folder inside assets and style.css is the css file. Like so:

   application
   assets
          css
              style.css
   system

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...