簡體   English   中英

Codeigniter資產文件夾的最佳實踐

[英]Codeigniter assets folder best practice

我對codeigniter相當新,但我學得很好,我要添加一個css,images,js,...文件夾,但我不知道在哪里放它

有人告訴我要制作一個“公共”文件夾

system
application
public
    css
    images

然后在你的index.php(在公共文件夾中)進行相應的調整

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

但是當我這樣做時,我得到了404(不是ci 404,而是一個真正未找到的)

任何人都知道我可能做錯了什么? 謝謝!

我有這個設置:

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]

現在您可以簡單地包含外部腳本,css示例:

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

其中css是資產中的文件夾,style.css是css文件。 像這樣:

   application
   assets
          css
              style.css
   system

另一個角度 - 我認為他們試圖告訴你要做的 - 是將你的應用程序和系統文件夾從“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中,路徑向上一級“up”就像

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

獎金 - 這里有兩個技巧對我有很大幫助。 1)命名您的應用程序文件夾,然后您可以輕松地在不同版本之間切換,“快速回滾”等。

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

2)將基本URL放在index.php頁面上 - 而不是在配置中。

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

這樣你可以有一個本地開發版本,單獨的實時服務器版本 - 你永遠不必擔心編寫基本URL,因為它在index.php頁面上 - 而不是在配置中。

我會恢復你對index.php所做的更改,因為它讓我覺得它們是問題的根源。

我一直在為CodeIgniter使用類似的設置,我的所有圖像,CSS和JS都進入一個名為static的文件夾,而我所做的就是創建該文件夾並將文件放入其中,並且它們工作正常。 如果要從URL中刪除index.php ,則需要確保.htaccess文件不會重寫靜態文件的URL。

尋找簡單的資產助手來為您的應用程序加載任何東西..

codeigniter的簡單資產助手

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM