티스토리 뷰

코드이그나이터(codeigniter) 개발 및 실습 환경 세팅

 

오랜만에 php를 하는데 내가 일단은 php를 별로 좋아하진 않는데 코드이그나이터를 넣으면 그래도 좀 할만해진다. 그런데 이 {} 중괄호를 엔터치고 쓰는 방식을 이거 개발자님께서 좋아하시는 것 같아서 여전히 좀 맘에 안들긴 하지만 암튼 나의 고갱님들께서 이걸로 고민을 하시길래 알려준다고 하면서 오랜 기억을 더듬으며 쓴다.

 

이건 개발환경이고 운영 환경은 좀 더 세심하게 세팅 해야하지만 일단 ci를 해볼려면 환경 구축을 빨리 하는게 유리하기 때문에 최대한 간단하게 할 수 있는 방법으로 소개해본다.

 

 

구조

운영에는 /var/www/html/이 root이다.

local에는 c:\git/project_name/이 root다.

 

 

진행 순서

1.wnmp다운

wnmp는 서버, php, db를 한방에 띄워주고 연동까지 해준다.

https://www.getwnmp.org/ 여기서 wnmp를 받고 아무데나 압축을 푼다. 나는 바탕화면에 풀었다.

 

2.코드이그나이터 다운

https://codeigniter.com/ 들어가서 코드이그나이터를 받는다.

 

3.wnmp를 압축푼 폴더에 html폴더 안에 코드이그나이터 다운받은걸 압축을 푼다.

이걸 한줄로 써놓았지만 정확하게 해야 하는 과정이다.

 

상단에 경로를 보면서 참고하기 바란다.

 

 

wnmp를 켜면 위 화면이 나오는데 Nginx -> Configuration ->nginx.conf 를 누른다.

 

아래 포스트와

 

2017/01/11 - [Web개발/PHP] - nginx에서 php ci(code igniter) 설정

 

아래 코드를 보면서 nginx.conf설정을 한다.

 

# Begin HTTP Server
server {
    listen 80; # IPv4
    server_name localhost;

    ## Parametrization using hostname of access and log filenames.
    access_log logs/localhost_access.log;
    error_log logs/localhost_error.log;

    ## Root and index files.
    root html;
    index  index.php index.html index.htm;

    ## If no favicon exists return a 204 (no content error).
    location = /favicon.ico {
        try_files $uri =204;
        log_not_found off;
        access_log off;
    }
        
    ## Don't log robots.txt requests.
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    ## Try the requested URI as files before handling it to PHP.
    location / {

        ## Regular PHP processing.
        location ~ \.php$ {
            try_files  $uri =404;
            fastcgi_pass   php_processes;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        ## Static files
        location ~* \.(?:css|gif|htc|ico|js|jpe?g|png|swf)$ {
            expires max;
            log_not_found off;
            ## No need to bleed constant updates. Send the all shebang in one
            ## fell swoop.
            tcp_nodelay off;
            ## Set the OS file cache.
            open_file_cache max=1000 inactive=120s;
            open_file_cache_valid 45s;
            open_file_cache_min_uses 2;
            open_file_cache_errors off;
        }

        ## Keep a tab on the 'big' static files.
        location ~* ^.+\.(?:ogg|pdf|pptx?)$ {
            expires 30d;
            ## No need to bleed constant updates. Send the all shebang in one
            ## fell swoop.
            tcp_nodelay off;
        }
        } # / location
    if (!-e $request_filename ) {
          rewrite ^(.*)$ /index.php last;
    }

} 
# End HTTP Server

 

 
if (!-e $request_filename ) {
  rewrite ^(.*)$ /index.php last;
}

 

위 세줄을 잘 넣으면 된다.

 

저장하고 nginx를 재시작 한다.

 

이걸 안하면 Controller를 추가 해도 잘 안되는걸 경험 할 수 있기 때문에 잘 안되면 이 부분을 반드시 확인해보기 바란다.

 

웹브라우저에서 localhost/welcome 을 치고 들어 가거나 localhost로 치고 들어가면 잘 설치 되었다면 아래 화면이 나온다.

 

idea로 열던지 편집기로 열던지 해서 application/controller/Welcome.php 를 복사해서 Main.php를 만든다.

웰컴을 이렇게 바꿔보고

 


실행하면 이렇게 나오는지 확인하자

 

 

autoload.php에 'database'를 로드한다. 그래야 db가 된다.

 

 

모델은 아래와 같이 만든다.

application/models/User.php

<?php
class User extends CI_Model {
    public function __construct()
    {
        parent::__construct();
        // Your own constructor code
    }

    public function select()
    {
        $result = $this->db->query('select * from users');
        return $result;
    }
}
 
선언은 아래와 같이 해준다.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Main extends CI_Controller {
    public function index()
    {
        //$this->load->view('welcome_message');
        echo "hello main";
    }

    public function hello()
    {

        $this->load->model('User');
        $result = $this->User->select();
        var_dump($result);
        echo "main -> hello";
    }
}
 

 

호출은

localhost/main/hello 이렇게 한다.

 

 

jquery ajax로 call하기

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script
            src="https://code.jquery.com/jquery-2.2.4.min.js"
            integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
            crossorigin="anonymous"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                url: "/main/hello",
            }).done(function(data) {
                console.log(data);
            });
        });
    </script>
</head>
<body>
    hello
</body>
</html>
 

 

 

 

end.

 

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함