Professional Documents
Culture Documents
web programming
how to develop a professional web base application
using yii framework in one week
by hafid mukhlasin
whats yii?
10
whats yii?
salah satu framework PHP populer, berupa kumpulan kode dan
library PHP yang membentuk sebuah kerangka dasar untuk
membangun berbagai jenis aplikasi berbasis web, misalnya: web
portal, forum, CMS, e-commerce, web service, dll
official site: http://www.yiiframework.com
Wednesday, January 13, 2016
11
history of yii?
mulai dikembangkan 2008, oleh Qiang Xue
versi 1.x 2008 2014 disupport hingga akhir 2015
versi 2.x oktober 2014 sekarang
latest version: 2.0.6 (august 2015)
Wednesday, January 13, 2016
12
14
15
16
on linux
export PATH=${PATH}:/opt/lampp/php/bin
asumsi: file php executablenya ada di /opt/lampp/php/bin
Wednesday, January 13, 2016
17
php composer
dependency manager untuk php, yaitu sebuah tools yang akan memudahkan
developer dalam menginstall suatu library (framework, cms, dll) berbasis php
beserta library dependensinya.
- perlu koneksi internet
- https://getcomposer.org
18
19
composer installation
on windows
php -r "readfile('https://getcomposer.org/installer');" | php
setx /M PATH "%PATH%;C:\Users\hafid"
perintah kedua supaya kita bisa mengakses composer dari lokasi manapun pada komputer
maka tambahkan current folder ke PATH (asumsi: C:\Users\hafid)
on linux
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
perintah kedua supaya kita bisa mengakses composer dari lokasi manapun pada komputer
maka pindahkan ke direktori global
Wednesday, January 13, 2016
20
21
yii installation
22
yii installation
online installation
- using php composer
- recommendate
offline installation
- using downloadable source
23
24
25
online installation
26
online installation
composer global require "fxp/composer-asset-plugin:~1.0.0
composer create-project --prefer-dist yiisoft/yii2-app-basic basic
27
offline installation
28
offline installation
Download source Yii 2.0 dalam bentuk file terkompresi dari
http://www.yiiframework.com/download/
http://local (customize app template)
Extract source tersebut web root yaitu htdocs atau www atau
public_html.
Edit file config/web.php dan masukkan secret key yang digunakan
untuk konfigurasi cookieValidationKey:
'cookieValidationKey' => 'masukkan kata kunci rahasia
disini',
29
configuration
30
configuration
database configuration
database migration
31
database configuration
32
database configuration
1. create new database for example yii2workshop
2. adjust database configuration in file config/db.php
33
database migration
34
database migration
automatic create user table, yii migrate
35
36
done!
37
yii architecture
38
yii architecture
structure directory project
files on root directory
application flow
routing concept
39
contains
assets/
assets definition
commands/
config/
controllers/
application configurations
web controller classes
mail/
migrations/
database migrations
models/
model classes
runtime/
tests/
vendor/
views/
web/
Wednesday, January
13, 2016
widgets/
40
41
application flow
42
43
default controller
ketika url yang diakes adalah
index.php saja maka:
controller default adalah
app/controllers/SiteController
action default dari SiteController
adalah actionIndex
artinya yii akan menjalankan
action ini.
index.php?r=site/index
Wednesday, January 13, 2016
44
default layout
perintah pada fungsi actionIndex
akan merender :
- default layouts yaitu main.php
yang ada di folder:
app/views/layouts/
- tampilan content index.php yang
ada pada folder:
app/views/site/
45
routing concept
controller
format : NamaController
url addr : index.php?r=nama
example:
controller
url
(format StudlyCaps)
(format lowercase)
SiteController
index.php?r=site
SiteController
example:
method
actionOke
index.php?r=site/hello-word
url
index.php?r=site/oke
index.php?r=site%2Fhello-word
Wednesday, January 13, 2016
46
mvc concept
47
whats mvc?
mvc: konsep dalam pengembangan aplikasi modern yaitu membagi
aplikasi menjadi 3 bagian yaitu:
model, format struktur data (database)
view, format tampilan data ke user (user interface)
controller, bisnis proses tentang bagaimana data ditampilkan (logic)
Wednesday, January 13, 2016
48
why mvc?
Struktur aplikasi akan lebih rapi dan mudah difahami terutama untuk
project yang kompleks
Lebih mudah ketika terjadi perubahan data, bisnis proses maupun
tampilan
Lebih mudah dalam tracking dan handling error
Mudah dalam membagi pekerjaan jika project dikerjakan oleh team
Wednesday, January 13, 2016
49
50
mvc on practice!
51
1. controller only
on SiteController, add new action
public
{
e
}
52
2. view + controller
on SiteController, modify actionHalo
publi
{
re
}
53
<?php
namespa
use yii
class G
{
publi
publi
{
re
[
'requir
];
}
}
54
public f
{
$mod
retu
]);
55
<?php
use yi
use yi
$form
echo $
echo $
echo $
echo H
'cl
Active
?>
56
57
public
{
$mod
if(Y
$
i
}
retu
'
]);
}
Wednesday, January 13, 2016
58
59
prepare table
CREATE TABLE IF NOT EXISTS `comment` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`message` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
60
model comment
Care about case
create app/models/Comment.php
<?php
name
use y
class
public
return
}
public
return
}
}
Characteristic:
- use class ActiveRecord
- function tableName()
- field autoIncrement dont add to rules
required
61
controller comment
create app/controllers/CommentController.php
<?p
nam
use
use
use
clas
{
/
}
62
63
actionIndex
add function actionIndex to CommentController
publ
// SE
$mo
retu
'mo
]);
}
64
view index.php
<table
<tr>
<th>N
<th>N
<th>M
</tr>
<?php
foreach
echo
echo
echo
echo
echo
}
?>
</table
65
try it
access URL index.php?r=comment/index
66
67
actionCreate
add function actionCreate
to CommentController
- read data user form submit to model
publ
$mod
proce
>requ
>load
if($m
Yii::$
>setF
telah
else{
>setF
gagal
retur
'mod
$model->load(Yii::$app->request->post());
68
<?
use
use
$fo
ech
ech
ech
'c
]);
Ac
?>
69
try it
index.php?r=comment/create
70
update data
71
72
<?
us
?>
<t
<t
<
<
<
<
</
<?
fo
e
73
}
?>
</
actionUpdate
add function actionUpdate
to CommentController to handle link update
from index
pu
Com
$m
>sa
'da
>se
}
$m
index.php?comment/update&id=2
- use parameter id as variabel in function
public function actionUpdate($id)
74
<?
use
use
$fo
ech
ech
ech
'c
]);
Ac
?>
75
try it
index.php?r=comment/index
76
delete data
77
78
<?p
use
?>
<tab
<tr>
<th
<th
<th
<th
</tr
<?p
fore
ec
e
e
e
e
e
e
e
]
e
ec
79
actionDelete
add function actionDelete
to CommentController to handle link delete
from index
publ
Comm
>sess
// red
}
80
try it
index.php?r=comment/index
81
Thank You
http://www.hafidmukhlasin.com
82