시작하기

* 프로그램의 첫 단추 hello world로 시작해 봅시다.
<?php
/**
 * Hello World
 *
 * Created on 2014. 10. 13.
 * @author 불의회상 <hoksi2k@hanmail.net>
 * @version 1.0
 */
class Hello_world extends CI_Controller {
	public function index() {
		echo 'Hello World!';
	}

}

* World가 지겹다면 Controller로 바꿔 봅시다.

갑자기 $name 이 생기기는 했지만 신경쓰지 맙시다.

<?php
/**
 * Hello World
 *
 * Created on 2014. 10. 13.
 * @author 불의회상 <hoksi2k@hanmail.net>
 * @version 1.0
 */
class Hello_controller extends CI_Controller {
	public function index($name = 'Controller') {
		echo 'Hello ' . $name . '!';
	}

}


* 내 이름이 안나오니 별로네요.

'hoksi' 가 출력되도록 수정해 봅시다. 

하지만 프로그램을 수정하지는 않을겁니다. 이럴때를 위해서 $name 을 추가 한거니까요. ^^

대신 아래 처럼 URL에 인자값을 넘겨주면 됩니다.

갑자기 index 가 붙기는 했지만 출력은 잘 된답니다.

ex> hello_controller/index/hoksi

<?php
/**
 * Hello World
 *
 * Created on 2014. 10. 13.
 * @author 불의회상 <hoksi2k@hanmail.net>
 * @version 1.0
 */
class Hello_controller extends CI_Controller {
	public function index($name = 'Controller') {
		echo 'Hello ' . $name . '!';
	}

}
* _remap을 사용해 봅시다
<?php
/**
 * Hello Remap
 *
 * Created on 2014. 10. 13.
 * @author 불의회상 <hoksi2k@hanmail.net>
 * @version 1.0
 */
class Hello_remap extends CI_Controller {
	public function _remap($name = 'World') {
		echo 'Hello ' . $name . '!';
	}

}

* _remap을 사용하면 index를 지울 수 있습니다.

아래처럼요.. ^^

ex> hello_remap/hoksi

 

<?php
/**
 * Hello Remap
 *
 * Created on 2014. 10. 13.
 * @author 불의회상 <hoksi2k@hanmail.net>
 * @version 1.0
 */
class Hello_remap extends CI_Controller {
	public function _remap($name = 'World') {
		echo 'Hello ' . $name . '!';
	}

}
* 모델을 사용해 볼까요?
<?php
/**
 * Hello Model
 *
 * Created on 2014. 10. 13.
 * @author 불의회상 <hoksi2k@hanmail.net>
 * @version 1.0
 */
class Hello_model extends CI_Controller {
	public function index() {
		$this->load->model('mvc_sample/hello_m');
		
		echo 'Hello ' . $this->hello_m->get_name() . '!';
	}

}
<?php
/**
 * Hello_m
 *
 * Created on 2014. 10. 13.
 * @author 불의회상 <hoksi2k@hanmail.net>
 * @version 1.0
 */
class Hello_m extends CI_Model {
	public function __construct()
	{
		parent::__construct();
	}
	
	public function get_name()
	{
		return 'Model';
	}
}
* 뷰도 사용해 봅시다.
<?php
/**
 * Hello View
 *
 * Created on 2014. 10. 13.
 * @author 불의회상 <hoksi2k@hanmail.net>
 * @version 1.0
 */
class Hello_view extends CI_Controller {
	public function index() {
		$this->load->view('mvc_sample/hello_view');
	}

}
Hello view!
* 간단한 블러그도 만들어 봅시다.
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 블로그 기본 Controller
 *
 * Created on 2014. 10. 20.
 * @author 불의회상(hoksi2k@hanmail.net)
 * @version 1.0
 */
class Blog_basic extends CI_Controller {
    public function __construct() {
        parent::__construct();
 
 		$this->load->helper('url');
        $this->load->model('mvc_sample/blog_basic_model');
    }
     
    public function index() {
    	$data = array(
    		'blogs' => $this->blog_basic_model->blog_basic()
		);
		
        $this->load->view('mvc_sample/blog_basic', $data);
    }
	
	public function post() {
		$data = array(
			'title' => strip_tags($this->input->post('title', TRUE)),
			'content' => strip_tags($this->input->post('content', TRUE))
		);
		
		if($data['title'] != '' && $data['content'] != '') {
			//$this->blog_basic_model->insert($data);
		}
		
		redirect(site_url('mvc_sample/blog_basic'));
	}
}
<!DOCTYPE html>
<html lang="ko">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>블로그 기본</title>
        <meta name="author" content="불의회상(hoksi2k@hanmail.net)">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="/assets/bootstrap-2.3.2/css/bootstrap.min.css" media="screen" charset="utf-8" />
        <link rel="stylesheet" href="/assets/bootstrap-2.3.2/css/bootstrap-responsive.css" media="screen" charset="utf-8" />
        <link rel="stylesheet" href="/assets/font-awesome-3.2.1/css/font-awesome.min.css" media="screen" charset="utf-8" />
        <!-- Jquery -->
        <script src="/assets/jquery-1.8.3/jquery.min.js"></script>
        <!--[if IE 7]>
        <link rel="stylesheet" href="/assets/font-awesome-3.2.1/css/font-awesome-ie7.min.css" media="screen" charset="utf-8" />
        <![endif]-->
        <!--[if lt IE 9]>
        <script src="/assets/js/plugin/html5.js"></script>
        <![endif]-->
        <style>body {margin-top:60px;}</style>
    </head>
 
    <body>
	    <div class="navbar navbar-fixed-top">
	      <div class="navbar-inner">
	        <div class="container">
	          <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
	            <span class="icon-bar"></span>
	            <span class="icon-bar"></span>
	            <span class="icon-bar"></span>
	          </button>
	          <a class="brand" href="<?=site_url('mvc_sample/blog_basic')?>">Blog basic</a>
	          <div class="nav-collapse collapse">
	            <ul class="nav">
	              <li class="active"><a href="#">Home</a></li>
	              <li><a href="#post_blog" data-toggle="modal"><i class="icon-pencil"></i> Post</a></li>
	            </ul>
	          </div><!--/.nav-collapse -->
	        </div>
	      </div>
	    </div>

        <div class="container">
        	<?php foreach($blogs as $blog): ?>
            <div class="row">
                <div class="span12">
               	    <h3><i class="icon-beer"></i> <?=$blog['title']?></h3>
                </div>
                <div class="span12 well">
                	<?=nl2br($blog['content'])?>
                </div>
            </div>
            <?php endforeach ?>
        </div>
        
		<form action="<?=site_url('mvc_sample/blog_basic/post')?>" method="post">
		<div id="post_blog" class="modal hide fade">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h2>등록</h2>
			</div>
			<div class="modal-body">
			<fieldset>
				<div class="control-group">
				  <label class="control-label" for="title">제목*</label>
				  <div class="controls">
				    <input id="title" name="title" type="text" placeholder="" class="input-block-level" required="">
				  </div>
				</div>
				
				<div class="control-group">
				  <label class="control-label" for="content">내용*</label>
				  <div class="controls">                     
				    <textarea id="content" rows="10" name="content" class="input-block-level" required=""></textarea>
				  </div>
				</div>
			</fieldset>
			</div>
			<div class="modal-footer">
			    <button id="post_btn" class="btn btn-primary">등록</button>
			</div>
		</div>
		</form>        	
 
        <script src="/assets/bootstrap-2.3.2/js/bootstrap.min.js"></script>
    </body>
</html>
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 블로그 기본 Model
 *
 * Created on 2014. 10. 20.
 * @author 불의회상(hoksi2k@hanmail.net)
 * @version 1.0
 */
class Blog_basic_model extends CI_Model {
    protected $tbl;
     
    public function __construct() {
        parent::__construct();
         
        // $this->load->database(); // Database Load
        $this->tbl = 'blog_basic';
        $this->pkey = 'blog_basic_id';   
    }
    
	public function blog_basic() {
		$len = 10;
		
		return $this->db->order_by('created', 'desc')->limit($len)->get($this->tbl)->result_array();
	}
	
    public function insert($data) {
    	$this->db->set('created', 'now()', FALSE);
        $this->db->insert($this->tbl, $data);
		
		return $this->db->insert_id();
	}
}

~ To be continue...