You are on page 1of 8

Gii thiu

Bi trc mnh gii thiu ti cc bn bi Design Pattern - MVC


Pattern Hay M Hnh MVC (Link: http://www.stdio.vn/articles/read/155-gioithieu-mo-hinh-mvc). Hm nay, ti s th hin m hnh MVC trn ngn ng
lp trnh PHP thng qua mt v d c bn v thc hin cc php tnh cng,
tr.

Tin bi vit
Bi vit ny c ra i b tr kin thc cho vic tm hiu v lm
vic vi CodeIgniter framework

i tng hng n
Bi vit ny dnh cho tt c cc bn lp trnh vin khi mi bc vo
mng pht trin web s dng ngn ng PHP.

V d m hnh MVC v ngn ng PHP


Cu trc th mc ca v d

Trong th mc project demo c 3 th mc nh l model, controller


v view (hin thc ha ca m hnh MVC)

File index.php cha giao din form tng tc vi ngi dng.

Giao din ngi dng

Giao din ca form tng tc vi ngi dng.

Kt qu khi chng trnh thc hin php tnh.

Ni dung m ngun demo


-

File demo/index.php

<html>
<head>
<title>Calculation using MVC Model</title>
<style type="text/css">
#form{
min-width:300px;
max-width:600px;
margin:auto;
font-family: sans-serif;
box-shadow: 5px 5px 5px #888888;
padding:20px;
border:4px solid #27748A;
}

#form h2{
color:#27748A;
font-size:35px;
margin:0;
}

#form p{

font-size:15px;
color:#222222;
}
#form div
{
margin-top:10px;
}

#form input, textarea, button


{
width:50%;
border:2px solid #9ECEDB;
padding:3px 5px;
}

#form label
{
font-weight:bold;
font-size:12px;
color:#184552;
}
</style>
</head>

<body>
<form id="form" action="controller/CalcController.php"
method="post">
<h2>Calculation using MVC Model</h2>

<div>
<label for='name'>Number 1</label>
<input type='text' name='number1'
placeholder='number 1' required />
</div>
<div>
<label for='name'>Number 2</label>
<input type='text' name='number2'
placeholder='number 2' required />
</div>
<div>
<label for='name'>Method</label>
<select name="method_v">
<option value="add">Add</option>
<option value="sub">Sub</option>
</select>
</div>
<div>

<input type="submit" value="Send"


name="btnSubmit" style="width:30%"/>
</div>

</form>
</body>
</html>

File demo/controller/CalcModel.php
<?php
class CalcModel{
public $result;
public $a;
public $b;
public function add(){
$this->result = $this->a + $this->b;
}
public function sub(){
$this->result = $this->a - $this->b;
}
public function method_calc($method){
switch ($method) {
case 'add':
$this->add();
break;
case 'sub':
$this->sub();
break;
default:
$this->result = "No support";
break;
}
}
}

File demo/controller/CalcController.php
<?php
include_once '../model/CalcModel.php';
if(isset($_POST) AND isset($_POST['btnSubmit'])){
$var1 = $_POST['number1'];
$var2 = $_POST['number2'];
$method = $_POST['method_v'];
$calc = new CalcModel();
$calc->a = $var1;
$calc->b = $var2;
$calc->method_calc($method);
}
include_once('../view/CalcView.php');

File demo/controller/CalcView.php
<html>
<head>
<title>Calculation using MVC Model</title>
<style type="text/css">
#form{
min-width:300px;
max-width:600px;
margin:auto;
font-family: sans-serif;
box-shadow: 5px 5px 5px #888888;
padding:20px;
border:4px solid #27748A;
}
#form h2{
color:#27748A;
font-size:35px;
margin:0;
}
#form p{
font-size:15px;
color:#222222;
}
#form div

{
margin-top:10px;
}
#form input, textarea, button
{
width:50%;
border:2px solid #9ECEDB;
padding:3px 5px;
}
#form label
{
font-size:14px;
color:#184552;
}
</style>
</head>
<body>
<form id="form" action="controller/CalcController.php"
method="post">
<h2>Calculation using MVC Model</h2>
<div>
<label for='name'>Result: <strong><?php echo
$calc->result; ?></strong></label>
</div>
<form>
</body>
</html>

Ngun tham kho

You might also like