Programing

Codeigniter에서 이미지 파일, CSS, JS 등을 어디에 배치합니까?

lottogame 2020. 6. 22. 07:50
반응형

Codeigniter에서 이미지 파일, CSS, JS 등을 어디에 배치합니까?


CSS 폴더와 이미지 파일 폴더를 어디에 넣을 수 있습니까? 뷰 폴더 안에서 생각하고 있었습니까? 그러나 컨트롤러는 항상 기본 URL의 경로를 다시 라우팅하므로 .html 파일의 경로를 지정해야합니다. 이는 중복됩니다.


다음과 같은 설정이 있습니다.

  • 신청
  • 체계
  • 자산
    • js
    • imgs
    • CSS

그런 다음 설정에 따라 단순히 전체 경로를 반환하는 도우미 함수가 있습니다.

application / helpers / utility_helper.php :

function asset_url(){
   return base_url().'assets/';
}

나는 보통 같은 파일에 공통 루틴을 유지하고 codeigniter의 자동로드 구성으로 자동로드합니다.

참고 : base_url()액세스를 위한 자동로드 URL 도우미 .

application / config / autoload.php :

$autoload['helper'] = array('url','utility');

그러면 asset_url()코드 전체에 액세스 할 수 있습니다.


아니요, views 폴더 내부는 좋지 않습니다.

보기 : 프로젝트에 3 개의 기본 폴더가 있어야합니다.

system // CI 프레임 워크이므로이 파일을 터치 할 이유가별로 없습니다

application // 논리가가는 곳, 어플리케이션을 만드는 파일 ,

공개 // 이것은 문서 루트 여야합니다

보안상의 이유로 프레임 워크와 응용 프로그램을 documentroot 외부에 유지하는 것이 좋습니다 (public_html, htdocs, public, www ... 등).

공용 폴더 안에는 브라우저가 볼 수있는 공용 정보, 폴더를 찾기위한 공통 정보 (image, js, css)를 넣어야합니다. 따라서 구조는 다음과 같습니다.

|- system/
|- application/
|---- models/
|---- views/
|---- controllers/
|- public/
|---- images/
|---- js/
|---- css/
|---- index.php
|---- .htaccess

이것이 내가 공공 자산을 처리하는 방법입니다. 여기서는 Phalcon PHP 의 Bootstrap 방법을 사용합니다.

폴더 구조

|-.htaccess*
|-application/
|-public/
  |-.htaccess**
  |-index.php***
  |-css/
  |-js/
  |-img/
  |-font/
  |-upload/
|-system

편집 할 파일

  1. .htaccess*

    All requests to the project will be rewritten to the public/ directory making it the document root. This step ensures that the internal project folders remain hidden from public viewing and thus eliminates security threats of this kind. - Phalconphp Doc

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule  ^$ public/    [L]
        RewriteRule  (.*) public/$1 [L]
    </IfModule>
    
  2. .htaccess**

    Rules will check if the requested file exists and, if it does, it doesn’t have to be rewritten by the web server module. - Phalconphp Doc

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php/$1 [QSA,L]
    </IfModule>
    
  3. index.php***

    Modify the application path and the system path as,

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

Now you use the public folder as base_url()."public/[YOUR ASSET FOLDER]"

Hope this helps :)


I usually put all my files like that into an "assets" folder in the application root, and then I make sure to use an Asset_Helper to point to those files for me. This is what CodeIgniter suggests.


No one says that you need to modify the .htacces and add the directory in the rewrite condition. I've created a directory "public" in the root directory alongside with "system", "application", "index.php". and edited the .htaccess file like this:

RewriteCond $1 !^(index\.php|public|robots\.txt)

Now you have to just call <?php echo base_url()."/public/yourdirectory/yuorfile";?> You can add subdirectory inside "public" dir as you like.


Regardless of where you put the CSS file you can simply use the CodeIgniter "html" helper called link_tag(). You would apply this helper something like this:

echo link_tag('folder/subfolder/stylesheet.css');

It is up to you to locate the CSS file in the most appropriate place (in your opinion). You would have to either autoload the "html" helper or separately load it in to use it.

People keep suggesting the usage of base_url() to achieve this but it is actually not really the "CodeIgniter" way to do it (I think this question has a few duplicates). That will work but one of the reasons for using a framework is to use the pre-made functions within it.

There are many helpers in codeigniter to do this kind of thing.


add one folder any name e.g public and add .htaccess file and write allow from all it means, in this folder your all files and all folder will not give error Access forbidden! use it like this

<link href="<?php echo base_url(); ?>application/public/css/style.css" rel="stylesheet" type="text/css"  />
<script type="text/javascript" src="<?php echo base_url(); ?>application/public/js/javascript.js"></script>

I'm using the latest version of CodeIgniter 3.1.0. The folder structure of it is:

  • system
  • application
  • user_guide
  • assets
    • images
    • js
    • css

That's where you should put the images, css and js files inside the assets folder.


(I am new to Codeigniter, so I don't know if this is the best advice)

I keep publicly available files in my public folder. It is logical for them to be there, and I don't want to use Codeigniter for something I don't need to use it for.

The directory tree looks likes this:

  • /application/
  • /public/
  • /public/css/
  • /public/img/
  • /public/js/
  • /system/

The only files I keep in /public/ are Codeigniter's index.php, and my .htaccess file:

RewriteEngine On
RewriteBase /

RewriteRule ^css/ - [L]
RewriteRule ^img/ - [L]
RewriteRule ^js/ - [L]

RewriteRule ^index.php(.*)$ - [L]
RewriteRule ^(.*)$ /index.php?/$1 [L]

I use the following folder structure:

application 
system 
static
    admin
        js
        css
        images
    public
        js
        css
        images
    uploads
        original
        thumbs

you can put the css folder inside the assest folder(you name it any name) in the directory of your project as:
ci_app

application
views
assets

         css 
             style.css 

... when you want to load that file in a page, you can use base_url()function as this way: <head> <link rel='stylesheet' href='<?php echo base_url();?>assets/css/style.css'> </head> and you are sure to add base_url of your project in the config.php file as this:

$config['base_url'] = 'http://localhost/ci_app';

Hi our sturucture is like Application, system, user_guide

create a folder name assets just near all the folders and then inside this assets folder create css and javascript and images folder put all your css js insiide the folders

now go to header.php and call the css just like this.

<link rel="stylesheet" href="<?php echo base_url();?>assets/css/touchTouch.css">
  <link rel="stylesheet" href="<?php echo base_url();?>assets/css/style.css">
  <link rel="stylesheet" href="<?php echo base_url();?>assets/css/camera.css"> 

The link_tag() helper is clearly the way to do this. Put your css where it usually belongs, in site_root/css/ and then use the helper:

From The CodeIgniter docs...

echo link_tag('css/mystyles.css');

Output

<link href="http://example.com/css/mystyles.css" rel="stylesheet" type="text/css" />

참고URL : https://stackoverflow.com/questions/6630770/where-do-i-put-image-files-css-js-etc-in-codeigniter

반응형