我对codeigniter相当陌生,但我学得很好,我将添加一个css,images,js,...文件夹,但我不确定把它放在哪里
有人告诉我创建一个"公共"文件夹
system
application
public
css
images
然后在您的索引中.php(在公用文件夹中)相应地调整
$system_path = '../系统';$application_path = '../应用程序';
但是当我这样做时,我得到一个 404(不是 ci 404,而是一个真正没有找到的)
有人知道我可能做错了什么吗?谢谢!
我有这个设置:
application
assets
system
.htaccess
在"资产"文件夹中,我有"img"或"js"等子文件夹。我还使用"实用程序助手"来帮助我指向该文件夹。
如果要尝试,必须先创建一个名为"utility_helper.php"的帮助程序使用此代码:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('asset_url()'))
{
function asset_url()
{
return base_url().'assets/';
}
}
并将其存储在
application/helpers/
然后,您必须自动加载该帮助程序,然后转到:
application/config/autoload.php
并自动加载帮助程序(例如:)
$autoload['helper'] = array('form', 'url', 'utility');
您还必须路由到该文件夹("应用程序/配置/路由.php")
$route['assets/(:any)'] = 'assets/$1';
并有一个包含以下内容的 .htaccess 文件:
RewriteEngine on
RewriteCond $1 !^(index'.php|images|assets|robots'.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
现在您可以简单地包含外部脚本,CSS 示例:
<link rel="stylesheet" type="text/css" href="<?php echo asset_url();?>css/style.css">
其中 CSS 是资源和样式中的文件夹.css是 CSS 文件。这样:
application
assets
css
style.css
system
另一个角度 - 我认为他们试图告诉你做的 - 是将应用程序和系统文件夹从"公共"HTML文件夹上一级。 这样您的源文件就无法访问。 所以就像在Mamp中一样,公共文件夹被称为htdocs,在大多数Web托管中称为HTML。
/application/html/
/system /html/
// your main index page is in the public html folder
/.….. /html/index.php
// an assets folder in the public html folder with css, img, etc folders
/.…… /html/assets/css/
/.…… /html/assets/img/
然后在您的索引.php中,路径将"向上"一级,例如
$system_path = '../system';
$application_folder = '../application';
奖金 - 这里有两个对我有很大帮助的提示。1)命名您的应用程序文件夹,然后您可以轻松地在不同版本之间切换,"回滚"非常快,等等。
/app_alpha/html/
/app_beta01/html/
/app_beta02/html/
2)将基本URL放在索引.php页面上 - 而不是在配置中。
$assign_to_config['base_url'] = 'https://awesomedomain.com';
这样你就可以有一个本地开发版本,一个单独的实时服务器版本 - 你永远不必担心过度编写基本URL,因为它在索引.php页面上 - 而不是在配置中。
我会恢复您对index.php
所做的更改,因为在我看来它们是问题的根源。
我一直在为CodeIgniter使用类似的设置,我的所有图像,CSS和JS都放在一个名为static
的文件夹中,实际上我所做的只是创建该文件夹并将文件放入其中,它们工作正常。如果要从 URL 中删除index.php
,则需要确保.htaccess
文件不会重写静态文件的 URL。