Codeigniter资产文件夹的最佳实践

时间:2013-11-06 18:50:49

标签: php codeigniter

我对codeigniter很新,但我学得很好,我要添加一个 css,images,js,... 文件夹,但我不确定在哪里把它

有人告诉我要制作一个'公共'文件夹

system
application
public
    css
    images

然后在index.php(在公共文件夹中)进行相应的调整

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

但是当我这样做时,我得到了404(不是ci 404,而是一个真正未找到的)

任何人都知道我可能做错了什么?谢谢!

4 个答案:

答案 0 :(得分:52)

我有这个设置:

application
assets
system
.htaccess

在“assets”文件夹中我有“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');

您还必须路由到该文件夹​​('application / config / routes.php')

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

并拥有包含此内容的.htaccess文件:

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

现在您可以简单地包含外部脚本,例如:

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

其中css是资产内的文件夹,style.css是css文件。 像这样:

   application
   assets
          css
              style.css
   system

答案 1 :(得分:10)

另一个角度 - 我认为他们试图告诉你的事情 - 是你的应用程序&amp;系统文件夹从“public”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/

然后在你的index.php中,路径就像

一样“向上”
$system_path = '../system';
$application_folder = '../application';

奖金 - 这里有两个技巧对我有很大帮助。 1)命名您的应用程序文件夹,然后您可以轻松地在不同版本之间切换, “快速回滚”,等等。

 /app_alpha/html/
 /app_beta01/html/
 /app_beta02/html/

2)将基本网址放在index.php页面上 - 而不是在配置中。

   $assign_to_config['base_url'] = 'https://awesomedomain.com'; 

通过这种方式,您可以拥有本地开发版本,单独的实时服务器版本 - 而且您永远不必担心编写基本URL,因为它位于index.php页面上 - 而不是在配置中。

答案 2 :(得分:1)

我会将您对index.php所做的更改还原,因为它让我觉得它们是问题的根源。

我一直在使用类似的CodeIgniter设置,我的所有图像,CSS和JS都放在一个名为static的文件夹中,我所做的就是创建该文件夹并将文件放入其中,他们工作得很好。如果您要从网址中删除index.php,则需要确保您的.htaccess文件不会重写静态文件的网址。

答案 3 :(得分:0)

寻找简单的资产助手来为您的应用程序加载任何东西..

simple assets helper for codeigniter