Path to assets folder on CodeIgniter

偶尔善良 提交于 2021-01-01 09:09:41

问题


I'm coding something into CodeIgniter and I want to apply a css template. I found one I like and download it.

After merging the css code with the code I have before the merging, I found that the images used by the css template doesn't load. Originally they should stay on root of html folder (or www, or public_html, you know what I mean...), but I put them under an assets folder which is on same level as system folder.

Something like this...

Website Folder
|
|---application\
|-----(CodeIgniter application folders...)
|---assets\
|-----style.css
|-----css\
|-------mini.css
|-----images\
|-----js\
|-----robots.txt
|---system
|-----(CodeIgniter system folder...)
|index.php

I googled for a couple of hours and I found this post (post #5). I try what the OP says but it doesn't work.

I can autoload the url_helper adding

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

to the autoload.php file. But when I add

<base href="<?php echo base_url() ?>"/>

the images are still absent.

The original html file has this line

<link href="style.css" type="text/css" rel="stylesheet" /> 

so, I'm guessing that I should add assets/ so it looks

<link href="assets/style.css" type="text/css" rel="stylesheet" />

but still the images are missing.

If I move the contents of assets folder to root, everything is fine, but of course, that's not a good practice AFAIK...

My base_url is

http://localhost/prog/nonsense/mvc/

Before you ask, yes, I did read the .htacces solutions, but I really don't want to mess with .htaccess editing for now.

A little help here could be appreciated.


回答1:


have you check .htaccess ? It must have something like:

RewriteCond $1 !^(index\.php|assets|upload|robots\.txt|.*\.css)



回答2:


I have the same thing as you, and I call them like this:

<link href="<?=base_url('assets/style.css');?>" rel="stylesheet">

This works for me if the base_url() is set and the url helper is called.

I call the base_url() and then the assets folder then style.css. There's no need for a / after base_url();?> because there's one in the base_url() anyway.

Another option using codeigniter's built in HTML function:

Also, if you look at this link, you'll see you can use the HTML helper and call it via codeigniter's built in functions.

This time, you shouldn't need base_url();

do:

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

then in the view for header add this:

<?=link_tag('assets/style.css');?>

It will output to this:

<link href="http://localhost/prog/nonsense/mvc/assets/style.css" rel="stylesheet" type="text/css" />



回答3:


base_url could also be used as link_tag:

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

would work too and keep your code cleaner.




回答4:


yes! I just added the following line in .htacces file it working fine

RewriteCond $1 !^(index\.php|images|robots\.txt)



回答5:


I just add one line in .htacces side of application folder

RewriteCond $1 !^(index\.php|images|robots\.txt)


来源:https://stackoverflow.com/questions/21303268/path-to-assets-folder-on-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!