Вы находитесь на странице: 1из 6

CODE

IGNITER
3
EKO HALIM
SANTOSO
Kamenhalim.

DAFTAR ISI
HALAMAN STATIS........................................................................................................ 3
Halaman Statis........................................................................................................ 3
Menambahkan Logika ke Controller........................................................................4
Routing.................................................................................................................... 4

Halim Soft

Page 2

HALAMAN STATIS
Halaman Statis
Contoh pemanggilan
http://example.com/news/latest/10
menampilkan 10 item dari database.

Model Pattern.
http://example.com/[controller-class]/[controller-method]/[arguments]

buat file
application/controllers/Pages.php
<?php
class Pages extends CI_Controller {
public function view($page = 'home')
{
}
}

Skrip diatas digunakan unutk membuat class pages. Dengan menerima 1 argumen
bernama $page
buat header at application/views/templates/header.php
<html>
<head>

<title>CodeIgniter Tutorial</title>

</head>
<body>
<h1><?php echo $title; ?></h1>

Buat footer at application/views/templates/footer.php


</body>

<em>&copy; 2015</em>

</html>

Halim Soft

Page 3

Menambahkan Logika ke Controller


Template halaman statis akan berada di direktori
application/views/pages/
DI dalam directory tersebut, buat 2 file
home.php dan about.php
Lalu untuk meload kedua halaman tersebut..
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter

$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);

Routing
Buka file routing yang terletak di application/config/routes.php
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';

Halim Soft

Page 4

HALAMAN DINAMIS
Seting model
Query harus diletakan di model.
Buka application/models/ dan buat file file
News_model.php
<?php
class News_model extends CI_Model {

public function __construct()


{
$this->load->database();
}

Sebelumnya buat schema database.


News.sql
CREATE TABLE news (
id int(11) NOT NULL AUTO_INCREMENT,
title varchar(128) NOT NULL,
slug varchar(128) NOT NULL,
text text NOT NULL,
PRIMARY KEY (id),
KEY slug (slug)
);

Dapatkan semua post dari database dengan Query Builder yang terdapat di CI 3
Tambahkan di model
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}

Kita sudah mendapatkan 2 query.


Halim Soft

Page 5

Tampilkan News
Buat Controller baru di application/controllers/News.php.
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
}

public function view($slug = NULL)


{
$data['news_item'] = $this->news_model->get_news($slug);
}

Lalu passing data ke view


public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}

Halim Soft

Page 6

Вам также может понравиться