Friday, September 9, 2011

CodeIgniter 시작 [I]

CodeIgniter 설치


PHP Development Framework인 CodeIgniter를 사용하는것을 정리하였다
인터넷에 설명서가 많지만 그 중에서 "신규하 블로그"를 참조하였는데, 그것이 작성될 때는 CodeIgniter의 버전이 1.7 이라, 지금의 2.0하고는 코딩에서 조금 차이가 있어서, 그것들만 고쳤다

1. 설치디렉토리

    CodeIgniter를 얻으면 압축파일로 되어 있고, 그 속에는 해당 버전의 디렉토리가 있다
   그것을 앞으로 CodeIgniter의 디렉토리로 쓸 곳으로 옮긴다

    EX: J:\myblog

    이 디렉토리는 apache에서 document_root 디렉토리이다
    만약 document_root 이하에 subdirectory를 만들고 그곳에 CI를 설치한다면 apache의 httpd-vhosts.conf 파일에서 추가 또는 변경한다

    례를 들면

    document_root directory: J:\www
    CI 설치 디렉토리: J:\www\myblog

    이 경우에 httpd-vhosts.conf 에 다음사항을 기재한다

    <VirtualHost *:80>
        DocumentRoot "j:/vhosts/myblog"
        ServerName myblog
    </VirtualHost>


    설치한 디렉토리의 내용은 2.0에서 좀 달라서, 1.7에서는 system 아래에 모든 것이 있었지만 2.0에서는 system 안의 application 디렉토리를 밖으로 끄집어 내어서 그 구조가 다음과 같이 된다 (여기에는 하위 디렉토리의 일부만 적었다)

    system
        core
        database
        helpers
        libraries
    application
        controllers
        config
        views
        models

2. 설치후 사용하기 전에 하는 설정

    Base Site URL, Database, Initial Loading Page, URL Rewrite 등을 먼저 변경해준다

    1) Base Site URL

        application/config/config.php
        $config['base_url'] = ''

        기본으로 아무것도 없이 ''로 되어 있어서, 설치디렉토리가 Base Site URL로 되어 있으므로 고치지 않고 그대로 둔다

        apache의 document root이 J:\www로 되어 있고, CI 설치디렉토리는 J:\www\myblog 이면 base_url을 'myblog'로 변경한다

    2) Database

        앞으로 사용할 Database를 정하여 만들고, Username, Password, Database Name 들을 적는다

        application/config/database.php
        $db['default']['hostname'] = 'localhost';
        $db['default']['username'] = 'xxxxxxxx';
        $db['default']['password'] = 'xxxxxxxx';
        $db['default']['database'] = 'helloworld';
        $db['default']['dbdriver'] = 'mysql';

    3) Database Variable

        CodeIgniter는 Database Class를 기본으로 자동으로 Loading하지 않아서, coding할 때 Database를 연결하여 사용하려면 먼저 Database Library를 연결하여야 한다
        그렇게 하지 않고, 처음부터 자동으로 loading하여 계속 사용하기 위하여 설정을 한다

        application/config/autoload.php
        $autoload['libraries'] = array('database');

    4) Data for Testing

        시험용 데이터를 만든다
        참조한 블로그에 있는 대로 사용하기로 하였다


CREATE TABLE `data`(
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(255) NOT NULL,
  `text` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

INSERT INTO `data` (`title`, `text`) VALUES('Hello World!', 'CodeIgniter is a powerful PHP framework with a very small footprint, built for PHP coders who need a simple and elegant toolkit to create full-featured web applications. If you''re a developer who lives in the real world of shared hosting accounts and clients with deadlines, and if you''re tired of ponderously large and thoroughly undocumented frameworks');

    5) Initial Loading Page

        CodeIgniter를 설치하자 마자 웹브라우저로 연결하면 Welcome 화면이 뜬다
        이것은 처음 시작하는 페이지를 application/controllers/welcome.php로 정했기 때문이다
        이것을 특정한 페이지로 만들려면 Default_Controller 를 바꾸고 application/controllers/ 에 새 controller를 만든다

        application/config/routes.php
        $route['default_controller'] = "helloworld";

        그리고 application/controllers/에 helloworld.php를 만든다
        (이미 welcome.php가 있으므로 참조해서 만든다)

    6) URL Rewrite

        CodeIgniter를 설치한 다음에 웹브라우저로 보면 default_controller를 부를 것이다
        그 다음에 다른 내용을 연결하려면 URL을 좀 별나게 http://codeigniter/index.php/otherpage.php 와 같이 해야 한다
        그 중간에 있는 index.php를 빼고 싶고, 또 웹 서버로 apache를 사용한다면 apache의 Rewrite 모듈을 사용하고, 또 codeigniter의 config를 설정한다

        먼저 설치디렉토리에 .htaccess 파일을 다음 내용으로 만든다
        J:\codeigniter\.htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]


        다음으로 config의 내용을 변경한다
        application/config/config.php


         $config['index_page'] = '';


        index.php로 되어 있는 것을 ''로 만든다


여기까지 하면 설치와 기본설정이 된 것이다
다음에는 간단하게 시헝코딩을 하여 Controller, View, Model을 살펴본다



No comments:

Post a Comment