php Codeigniter 资产文件夹最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19820314/
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 20:06:18  来源:igfitidea点击:

Codeigniter assets folder best practice

phpcodeigniter

提问by Miguel Stevens

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'; $application_path = '../application';

$system_path = '../system'; $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!

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

采纳答案by cartalot

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

另一个角度——我认为他们试图告诉你做的——是将你的应用程序和系统文件夹从“公共”html文件夹上移一级。这样您的源文件就无法访问。所以就像在 Mamp 中,公共文件夹称为 htdocs,在大多数网络托管中称为 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 中,路径将“向上”一级

$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 页面上——而不是在配置中。

回答by maetulj

I have this setup:

我有这个设置:

application
assets
system
.htaccess

and in the "assets" folder i have subfolders like "img" or "js" and so on. I also use "utility helper" to help me point to that folder.

在“assets”文件夹中,我有“img”或“js”等子文件夹。我还使用“实用程序助手”来帮助我指向该文件夹。

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/';

and have a .htaccess file with this content:

并有一个包含以下内容的 .htaccess 文件:

    RewriteEngine on
    RewriteCond  !^(index\.php|images|assets|robots\.txt)
    RewriteRule ^(.*)$ /index.php/ [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. Like so:

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

   application
   assets
          css
              style.css
   system

回答by Matthew Daly

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. If you want to remove index.phpfrom your URL's, you'll need to make sure that your .htaccessfile doesn't rewrite the URL's for your static files.

我一直在为 CodeIgniter 使用类似的设置,其中我所有的图像、CSS 和 JS 都放在一个名为 的文件夹中static,实际上我所做的就是创建该文件夹并将文件放入其中,并且它们工作正常。如果您想index.php从您的 URL 中删除,您需要确保您的.htaccess文件不会为您的静态文件重写 URL。

回答by xargr

look for simple assets helper to load anything to your application..

寻找简单的资产助手来将任何内容加载到您的应用程序中..

simple assets helper for codeigniter

codeigniter 的简单资产助手