简体   繁体   English

Codeigniter资产文件夹的最佳实践

[英]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 我对codeigniter相当新,但我学得很好,我要添加一个css,images,js,...文件夹,但我不知道在哪里放它

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 然后在你的index.php(在公共文件夹中)进行相应的调整

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

But when I do that i get a 404 ( not the ci 404, but a really-not-found one ) 但是当我这样做时,我得到了404(不是ci 404,而是一个真正未找到的)

Anyone has any idea what I might be doing wrong? 任何人都知道我可能做错了什么? Thanks! 谢谢!

I have this setup: 我有这个设置:

application
assets
system
.htaccess

and in the "assets" folder i have subfolders like "img" or "js" and so on. 在“assets”文件夹中我有“img”或“js”等子文件夹。 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: 如果你想尝试一下,你必须先用这段代码创建一个名为“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/';
       }
     }

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') 你还要路由到那个文件夹('application / config / routes.php')

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

and have a .htaccess file with this content: 并拥有包含此内容的.htaccess文件:

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

now you can simply include external scripts, css example: 现在您可以简单地包含外部脚本,css示例:

   <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. 其中css是资产中的文件夹,style.css是css文件。 Like so: 像这样:

   application
   assets
          css
              style.css
   system

another angle on this -- and i think what they were trying to tell you to do - is putting your application & system folders one level up from the "public" html folder. 另一个角度 - 我认为他们试图告诉你要做的 - 是将你的应用程序和系统文件夹从“public”html文件夹中提升一级。 that way your source files are not accessible. 这样你的源文件就无法访问。 so like in Mamp the public folder is called htdocs, in most web hosting its called 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/

then in your index.php, the path is going one level 'up' like 然后在你的index.php中,路径向上一级“up”就像

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

BONUS - here are two tips that have helped me a lot. 奖金 - 这里有两个技巧对我有很大帮助。 1) Name your application folder, then you can easily switch between different versions, 'roll back' very quickly, etc. 1)命名您的应用程序文件夹,然后您可以轻松地在不同版本之间切换,“快速回滚”等。

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

2) Put the base url on the index.php page -- not in the config. 2)将基本URL放在index.php页面上 - 而不是在配置中。

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

that way you can have a local development version, separate live server version -- and you never have to worry about over writing the base url because its on the index.php page - not in the config. 这样你可以有一个本地开发版本,单独的实时服务器版本 - 你永远不必担心编写基本URL,因为它在index.php页面上 - 而不是在配置中。

I would revert back the changes you made to your index.php , as it looks to me like they're the source of the problem. 我会恢复你对index.php所做的更改,因为它让我觉得它们是问题的根源。

I've been using a similar setup for CodeIgniter, whereby all my images, CSS, and JS go in a folder called static , and literally all I did was create that folder and put the files in it, and they worked fine. 我一直在为CodeIgniter使用类似的设置,我的所有图像,CSS和JS都进入一个名为static的文件夹,而我所做的就是创建该文件夹并将文件放入其中,并且它们工作正常。 If you want to remove index.php from your URL's, you'll need to make sure that your .htaccess file doesn't rewrite the URL's for your static files. 如果要从URL中删除index.php ,则需要确保.htaccess文件不会重写静态文件的URL。

look for simple assets helper to load anything to your application.. 寻找简单的资产助手来为您的应用程序加载任何东西..

simple assets helper for codeigniter codeigniter的简单资产助手

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM