php CodeIgniter 上资产文件夹的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21303268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:01:42  来源:igfitidea点击:

Path to assets folder on CodeIgniter

phpcsscodeigniter

提问by Ashir

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

我正在向 CodeIgniter 中编码一些东西,我想应用一个 css 模板。我找到了一个我喜欢的并下载了它。

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.

将css代码与我合并前的代码合并后,发现css模板使用的图片加载不出来。最初它们应该留在 html 文件夹的根目录(或 www 或 public_html,你知道我的意思......),但我将它们放在与系统文件夹相同级别的资产文件夹下。

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.

我用谷歌搜索了几个小时,我找到了这篇文章(第 5 篇文章)。我尝试了 OP 所说的但它不起作用。

I can autoload the url_helper adding

我可以自动加载 url_helper 添加

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

to the autoload.php file. But when I add

到 autoload.php 文件。但是当我添加

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

the images are still absent.

图像仍然不存在。

The original html file has this line

原来的html文件有这一行

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

如果我将资产文件夹的内容移动到根目录,一切都很好,但是当然,这不是一个好习惯 AFAIK ...

My base_url is

我的 base_url 是

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.

在你问之前,是的,我确实阅读了 .htacces 解决方案,但我现在真的不想弄乱 .htaccess 编辑。

A little help here could be appreciated.

在这里可以提供一些帮助。

回答by zerokavn

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

你检查了.htaccess吗?它必须有类似的东西:

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

回答by Albzi

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 urlhelper is called.

如果base_url()设置了并且url调用了助手,这对我有用。

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

我所说的base_url(),然后将assets文件夹,然后style.css。不需要/之后,base_url();?>因为base_url()无论如何都有一个。

Another option using codeigniter's built in HTML function:

使用 codeigniter 的内置 HTML 函数的另一个选项:

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

此外,如果您查看此链接,您会发现您可以使用HTML帮助程序并通过 codeigniter 的内置函数调用它。

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

这一次,你应该不需要 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" />

回答by Fabiano Araujo

base_url could also be used as link_tag:

base_url 也可以用作 link_tag:

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

would work too and keep your code cleaner.

也可以工作并保持您的代码更干净。

回答by mahamkali

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

是的!我刚刚在.htacces文件中添加了以下行,它工作正常

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

回答by alx

I just add one line in .htaccesside of application folder

我只是在 . 应用程序文件夹的htaccess一侧

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