CodeIgniter - Shofiq Sulaiman

Download Report

Transcript CodeIgniter - Shofiq Sulaiman

CodeIgniter
CodeIgniter is a powerful PHP framework with a
very small footprint, built for PHP coders who
need a simple and elegant toolkit to create fullfeatured web applications
Shofiq Sulaiman [[email protected]]
• HTML
<form method="POST" action="index.php">
<table> ….
• CSS
.input{
width:140px;….
• Javascript
<script>
function () {…
• PHP
<?php
$link = mysql_connect('localhost', 'root', 'sula');
mysql_select_db('mitm');…
• Query Database
INSERT INTO
buku_tamu (BukuTamuNama,BukuTamuEmail,BukuTamuIsi) …
• Perawatan
• Pembaharuan
• Tetapi, adakah
keunggulannya ?
Framework
• Adalah sekumpulan fungsi, class, dan aturan-aturan.
• Berbeda dengan library yang sifatnya untuk tujuan tertentu
saja, framework bersifat menyeluruh mengatur bagaimana
kita membangun aplikasi.
• Lebih Cepat, Lebih Baik.
Developer akan lebih fokus pada pokok permasalahan
(masalah koneksi database, form validation, GUI, security,
dsb telah disediakan oleh framework atau library).
• Teknologi tinggi berbiaya rendah
Alih-alih membangun semuanya sendirian, lebih baik
menggunakan resource yang sudah ada dan teruji
(Ancaman Sang Naga, Ming Zeng, hal 55)
• Model
Connects business objects and database tables to create a
persistable domain model where logic and data are presented in
one wrapping.
• View
Helper classes to display html elements in your views.
• Controller
Is made up of one or more actions that performs its purpose and
then either renders a template or redirects to another action. An
action is defined as a public method in the controller, which will
automatically be made accessible to the web-server through a
mod_rewrite mapping.
• The Model represents your data structures. Typically
your model classes will contain functions that help you
retrieve, insert, and update information in your
database.
• The View is the information that is being presented to
a user. A View will normally be a web page, but in
CodeIgniter, a view can also be a page fragment like a
header or footer. It can also be an RSS page, or any
other type of "page".
• The Controller serves as an intermediary between the
Model, the View, and any other resources needed to
process the HTTP request and generate a web page.
Keuntungan
• Struktur yang konsisten. Sangat berguna bila developer
banyak dan turnover tinggi.
• Struktur merupakan best practices. Semua sudah
ditempatkan di tempat yang paling sesuai.
• Dapat belajar tentang desain aplikasi yang baik.
• Hanya library dan helper yang dibutuhkan yang diload.
• Dokumentasi yang lengkap
Kerugian
• Butuh investasi waktu belajar dan adaptasi
• Web Server & Database
Apache, PHP, MySQL, WAMP, LAMP, AppServ, dsb
• Editor Code
PSPad, Dreamweaver, Notepad++, dsb
• Browser
IE, Firefox Mozila, Chrome, Opera, Safari, dsb
• CodeIgniter
CodeIgniter_1.7.0.zip
Jika belum ada sebagian atau seluruhnya belum
ada, anda dapat ambil di \\sulaiman\windows\
• Ekstrak CodeIgniter_1.7.0.zip dan langsung jalan 
1. The index.php serves as the front controller, initializing the base resources needed
to run CodeIgniter.
2. The Router examines the HTTP request to determine what should be done with it.
3. If a cache file exists, it is sent directly to the browser, bypassing the normal system
execution.
4. Security. Before the application controller is loaded, the HTTP request and any
user submitted data is filtered for security.
5. The Controller loads the model, core libraries, plugins, helpers, and any other
resources needed to process the specific request.
6. The finalized View is rendered then sent to the web browser to be seen. If caching
is enabled, the view is cached first so that on subsequent requests it can be
served.
• A Controller is simply a class file that is named in a way that can be
associated with a URI.
• The second segment of the URI determines which function in the
controller gets called.
• If your URI contains more then two segments they will be passed to your
function as parameters.
• A view is simply a web page, or a page fragment, like a header, footer,
sidebar, etc.
• To load a particular view file you will use the following function:
$this->load->view('name‘, $data);
• Models are PHP classes that are designed to work with information in
your database.
• To load a model you will use the following function:
$this->load->model('Model_name');
• helper file is simply a collection of functions in a particular category,
performs one specific task, with no dependence on other functions
• Loading a helper file is quite simple using the following function:
$this->load->helper('name');
• Auto-loading Helpers in application/config/autoload.php file
• Once you've loaded the Helper File containing the function you intend to
use, you'll call it the way you would a standard PHP function.
• Plugins work almost identically to Helpers
• plugin usually provides a single function, whereas a Helper is usually a
collection of functions
• Loading a plugin file is quite simple using the following function:
$this->load->plugin('name');
• Auto-loading Plugins in application/config/autoload.php file
• Menggunakan fungsi dalam library :
$this->someclass->some_function(); // Object instances will always be
lower case
•
<?php if ( ! defined('BASEPATH')) exit('No direct script
access allowed');
class Someclass {
function some_function() {
}
}
?>
• XSS Filtering
$config['global_xss_filtering'] = TRUE;
• Active Record
$query = $this->db->get('mytable');
// Produces: SELECT * FROM mytable
• Parser
<h5>{judul}</h5>
$this->parser->parse(‘judul', ‘CodeIgniter’);
• Fungsi
function SetTanggal($tanggal) {
$bln=array('Januari','Februari','Maret','April','Mei','Juni','Juli‘
,'Agustus‘, 'September','Oktober','November','Desember');
$tlen = sizeof($bln);
$tgl=explode('-',$tanggal);
for ($t=0;$t<$tlen;$t++) {
if ($tgl[1]==$t+1) $tgl[1]=$bln[$t];
}
$tanggal=$tgl[2].' '.$tgl[1].' '.$tgl[0];
return $tanggal;
}
• Pemakaian
$tanggal = SetTanggal(‘2009-05-03’);
• Class
class Waktu {
function Waktu() {
…
}
function SetWaktu($waktu) {
$wakt = explode(" ",$waktu);
$tanggal = $wakt[0];
…
return $tanggal.' '.$wakt[1];
}
}
• Pemakaian
$waktuObj = new Waktu();
$waktu = $waktuObj->SetWaktu(‘2009-05-03 12:13:20’);
CREATE DATABASE `mitm`;
USE `mitm`;
CREATE TABLE `buku_tamu` (
`BukuTamuId` int(10) NOT NULL auto_increment,
`BukuTamuNama` char(30) default NULL,
`BukuTamuEmail` char(50) default NULL,
`BukuTamuIsi` char(255) default NULL,
`BukuTamuWaktu` datetime default NULL,
PRIMARY KEY (`BukuTamuId`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
insert into
`buku_tamu`(`BukuTamuId`,`BukuTamuNama`,`BukuTamuEmail`,`BukuT
amuIsi`,`BukuTamuWaktu`) values
(1,'Sulaiman','[email protected]','Yang pertama nie','200905-23 13:21:52');
• Telah diberikan contoh untuk menampilkan data buku tamu dari
database di atas
• Dengan code di atas kita dapat melihat daftar buku tamu sekaligus
menambahkannnya
• Silahkan anda lengkapi untuk aksi edit dan delete data
• Setelah itu tambahkan paging
• Banyak hal dalam CodeIgniter yang belum
tersampaikan
• Mohon maaf dan terima kasih