Sunteți pe pagina 1din 82

web programming

how to develop a professional web base application


using yii framework in one week
by hafid mukhlasin

Wednesday, January 13, 2016

web programming
how to develop a professional web base application
using yii framework in one week
by hafid mukhlasin

Wednesday, January 13, 2016

app: native vs web

Wednesday, January 13, 2016

app: native vs web


native app
web app
aplikasi yang diinstall dan berjalan aplikasi yang berjalan pada web
secara langsung pada platform sistem browser dan tidak tergantung pada
operasi baik dekstop maupun mobile
sistem operasi
example: ms office, whatsapp
example: website, blog
programming: java, vb.net, c/c++
Wednesday, January 13, 2016

programming: php, asp.net, javascript


4

web programming using PHP

Wednesday, January 13, 2016

how to create web app?

Wednesday, January 13, 2016

how to create web app?


create from scratch
cms: drupal, wordpress, joomla, moodle
framework: yii, laravel, symfony, phalcon

Wednesday, January 13, 2016

why using framework


development time
security
standard
team work

Wednesday, January 13, 2016

Wednesday, January 13, 2016

whats yii?

Wednesday, January 13, 2016

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

features & advantages of yii


using mvc concept
simplify security standard
support many databases
integrated with user interface
framework (twitter bootstrap)
caching
restful web service
built-in code generator
Wednesday, January 13, 2016

many useful extensions


have application template
follow latest php technology &
standard (psr, namespace,
composer)
large community support (not
one man show)
clear documentation
etc
13

preparing before installation

Wednesday, January 13, 2016

14

required tool & environment

web server (apache or nginx)


php version 5.4 or later
code editor (notepad++, atom, sublime, etc)
dbms (mysql, postgree, oracle, etc) - optional
php as global path optional
php composer - optional

Wednesday, January 13, 2016

15

setting php as global path


variabel php yang bisa diakses secara global melalui terminal /
command prompt

Wednesday, January 13, 2016

16

setting php as global path


on windows
setx /M PATH "%PATH%;C:\xampp\php"

asumsi: lokasi file php.exe di c:\xampp\php\php.exe

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

Wednesday, January 13, 2016

18

composer, how to work?

Wednesday, January 13, 2016

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

before using composer


self update
php composer.phar self-update

set github token


get token from: https://github.com/settings/tokens/new
composer config -g github-oauth.github.com <oauthtoken>

Wednesday, January 13, 2016

21

yii installation

Wednesday, January 13, 2016

22

yii installation
online installation
- using php composer
- recommendate

Wednesday, January 13, 2016

offline installation
- using downloadable source

23

what will be installed?


1. core yii
2. application template
basic (single app)
advanced (multiple app, migration, login with database, etc)

Wednesday, January 13, 2016

24

we will use customization template


for workshop purpose, we will use hscstudio/yii2-app-workshop
its basic with advanced feature
+ yii2-widget
+ yii2-mimin
+ yii2-export
Wednesday, January 13, 2016

25

online installation

Wednesday, January 13, 2016

26

online installation
composer global require "fxp/composer-asset-plugin:~1.0.0
composer create-project --prefer-dist yiisoft/yii2-app-basic basic

change with below


composer create-project --prefer-dist hscstudio/yii2-app-workshop
workshop

Wednesday, January 13, 2016

27

offline installation

Wednesday, January 13, 2016

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',

Wednesday, January 13, 2016

29

configuration

Wednesday, January 13, 2016

30

configuration
database configuration
database migration

Wednesday, January 13, 2016

31

database configuration

Wednesday, January 13, 2016

32

database configuration
1. create new database for example yii2workshop
2. adjust database configuration in file config/db.php

Wednesday, January 13, 2016

33

database migration

Wednesday, January 13, 2016

34

database migration
automatic create user table, yii migrate

Wednesday, January 13, 2016

35

try it on your web browser!


http://localhost/workshop/web
try to access all pages
try to signup
try to login
enjoy it!
Wednesday, January 13, 2016

36

done!

Wednesday, January 13, 2016

37

yii architecture

Wednesday, January 13, 2016

38

yii architecture
structure directory project
files on root directory
application flow
routing concept

Wednesday, January 13, 2016

39

structure directory project


folder

contains

assets/

assets definition

commands/

console commands (controllers)

config/
controllers/

application configurations
web controller classes

mail/

view files for e-mails

migrations/

database migrations

models/

model classes

runtime/

contains files generated during runtime

tests/

various tests for the basic application

vendor/

dependent 3rd-party packages

views/
web/
Wednesday, January
13, 2016
widgets/

view files for the Web application


the entry script and Web resources
widgets files

40

files on root directory

.gitignore contains a list of directories ignored by git version system. If


you need something never get to your source code repository, add it
there.
composer.json - Composer config described in "Configuring Composer"
below.
init - initialization script described in "Configuration and environments"
below.
init.bat - same for Windows.
requirements.php - Yii requirements checker.
yii - console application bootstrap.
yii.bat - same for Windows.

Wednesday, January 13, 2016

41

application flow

Wednesday, January 13, 2016

42

overview entry script


file yang pertama kali diakses
app/web/index.php

Wednesday, January 13, 2016

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/

Wednesday, January 13, 2016

45

routing concept
controller
format : NamaController
url addr : index.php?r=nama
example:
controller

url

(format StudlyCaps)
(format lowercase)
SiteController
index.php?r=site

jika dua kata gunakan format Camel Case


example:
SiteController & actionHelloWord
index.php?r=site/hello-word

method action pada controller


format : actionNama
(format camelCase)
url addr : index.php?r=namacontroller/nama
(format lowerCase)
example:
url encoding
controller

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

Wednesday, January 13, 2016

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

mvc implementation on yii


mvc di yii tercermin dalam struktur aplikasinya, yaitu terdapat
folder:
- models (berisi semua file models),
- views (berisi semua file views), dan
- controllers (berisi semua file controller)
Wednesday, January 13, 2016

50

mvc on practice!

Wednesday, January 13, 2016

51

1. controller only
on SiteController, add new action

public
{
e
}

access url index.php?r=site/halo

Wednesday, January 13, 2016

52

2. view + controller
on SiteController, modify actionHalo

publi
{
re
}

create file halo.php at app/views/site/, Halo dunia :)


access url index.php?site/halo
done

Wednesday, January 13, 2016

53

3. model+ controller + view


create model GuestBook in app/models/GuestBook.php

Wednesday, January 13, 2016

<?php
namespa
use yii
class G
{
publi
publi
{
re
[
'requir
];
}
}

54

3. model+ controller + view


create actionGuestBook at SiteController

public f
{
$mod

retu
]);

Wednesday, January 13, 2016

55

3. model+ controller + view


create view form.php in app/views/site/

<?php
use yi
use yi
$form
echo $
echo $
echo $
echo H
'cl
Active
?>

Wednesday, January 13, 2016

56

3. model+ controller + view


try it index.php?site/guest-book

Wednesday, January 13, 2016

57

3. model+ controller + view


modify actionGuestBook at SiteController, check form submit and process it

public
{
$mod
if(Y
$
i

}
retu
'
]);
}
Wednesday, January 13, 2016

58

mvc with database

Wednesday, January 13, 2016

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;

Wednesday, January 13, 2016

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

Wednesday, January 13, 2016

61

controller comment
create app/controllers/CommentController.php

<?p
nam
use
use
use
clas
{
/
}

prepare skeleton controller


to add some function:
- select/read data
- insert/create data
- update data
- delete data
use app\models\Commet
Wednesday, January 13, 2016

62

select / read data

Wednesday, January 13, 2016

63

actionIndex
add function actionIndex to CommentController

publ
// SE
$mo
retu
'mo
]);
}

Wednesday, January 13, 2016

64

view index.php

<table
<tr>
<th>N
<th>N
<th>M
</tr>
<?php
foreach
echo
echo
echo
echo
echo
}
?>
</table

create file index.php in app/views/comment/

Wednesday, January 13, 2016

65

try it
access URL index.php?r=comment/index

Wednesday, January 13, 2016

66

insert / create data

Wednesday, January 13, 2016

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());

- save data to database


$model->save();

- show alert message


Yii::$app->session->setFlash('success',
'data telah tersimpan.');

Wednesday, January 13, 2016

68

create view create


app/views/comment/create.php

<?
use
use
$fo
ech
ech
ech
'c
]);
Ac
?>

Wednesday, January 13, 2016

69

try it
index.php?r=comment/create

Wednesday, January 13, 2016

70

update data

Wednesday, January 13, 2016

71

where will data updated?


update data base on primary key: id
step by step:
1. show all data
2. select one a data that will updated
3. show a data in form

Wednesday, January 13, 2016

72

<?
us
?>
<t
<t
<
<
<
<
</
<?
fo
e

show all data by modified


index.php
add column for link edit data in the tabel

create link use HTML helpers:


use yii\helpers\Html;
echo Html::a(Text,[abc/def]);
// <a href=index.php?r=abc/def>Text</a>
echo Html::a(Text,[abc/def,gh=>ij]);
// <a href=index.php?r=abc/def&gh=ij>Text</a>
Wednesday, January 13, 2016

73

}
?>
</

actionUpdate
add function actionUpdate
to CommentController to handle link update
from index

pu
Com
$m
>sa
'da
>se
}
$m

- url send parameter id

index.php?comment/update&id=2
- use parameter id as variabel in function
public function actionUpdate($id)

- get a comment base on id


$model = Comment::findOne($id);

Wednesday, January 13, 2016

74

create view update


app/views/comment/update.php

<?
use
use
$fo
ech
ech
ech
'c
]);
Ac
?>

Wednesday, January 13, 2016

75

try it
index.php?r=comment/index

Wednesday, January 13, 2016

76

delete data

Wednesday, January 13, 2016

77

where will data deleted?


delete data base on primary key: id
step by step:
1. show all data
2. select one a data that will deleted
3. action delete
4. back to index data

Wednesday, January 13, 2016

78

<?p
use
?>
<tab
<tr>
<th
<th
<th
<th
</tr
<?p
fore
ec
e
e
e
e
e
e
e

show all data by modified


index.php
add column for link delete data in the tabel
add parameter data-confirm
to show alert confirmation

Wednesday, January 13, 2016

]
e
ec

79

actionDelete
add function actionDelete
to CommentController to handle link delete
from index

publ
Comm
>sess
// red
}

- url send parameter id


index.php?comment/delete&id=2
- use parameter id as variabel in function
public function actionDelete($id)
- get a comment base on id
$model = Comment::findOne($id);
- delete a comment from database
$model->delete();

Wednesday, January 13, 2016

80

try it
index.php?r=comment/index

Wednesday, January 13, 2016

81

Thank You
http://www.hafidmukhlasin.com

Wednesday, January 13, 2016

82

S-ar putea să vă placă și