Amit Kumar Singh Itbrainshapers
Amit Kumar Singh Itbrainshapers
ITBrainShapers
MVC
Model
Data layer
View
Presentation layer
Controller
Logic layer
Typical Flow
2
4 Data
base
Script
Client
1 3
MVC Flow
MVC can vary depending on the framework with
which you’re working, but generally it works as follows
5. Once the model has pulled any data from or sent data
to the database, it returns its output to the controller.
Continue..
6. The controller processes the data and outputs to the
view file.
7. The view adds any design or display data to the
controller output and sends its output to the client’s
browser
WEB SERVER
request for
/tasks/index 7 Response
1 Request
TasksController Layout
{tasks_controll {tasks.th
er.php}
Index ToDo Tasks
tml}
Method Method Index
2 Index View
5
Index
Done Undo Tasks 6 View
View Tasks
Method Method Views combined
Index
selected Index
Metho with
View {index.thtml
3 d Data View layout
Data todo.thtml
4
Called
Requested returned done.thtml}
Model Task
{task.php}
DATABASE
Table tasks
MVC FLOW
CakePHP
A framework for developing applications in PHP
Inspired by Ruby on Rails
Follows MVC design pattern
Convention over configuration
CakePHP follows the MVC software design pattern.
Programming using MVC separates your application
into three main parts:
The Model represents the application data
The View renders a presentation of model data
The Controller handles and routes requests made by
the client
Typical Flow Of CakePHP
CakePHP (or, for short, Cake) is a framework, not a
set of libraries, even though it contains dozens of
functions and methods that simplify web
development much like libraries do.
Model
class Student save as student.php
controller
class StudentsController -students_controller.php
view
foldername =students
<?php
/* /app/model/hello.php */
class Hello extends AppModel {
var $name = 'Hello';
var $useTable = false;
}
?>
Hello world… again
View:
<!--
/* /app/views/index.thtml */
-->
<hr size=1/>
<h1><?php echo $data ?></h1>
<hr size=1/>
Controller:
<?php
/* app/controller/hello_controller.php */
class HelloController extends AppController {
var $name = "Hello";
var $uses = 'Hello';
function index() {
$data = 'Hello world!';
$this->set('data', $data);
}
}
?>
Simple DB table app
• An online contact list
• We want to add, edit, view and delete
names and phone numbers
• Uses a single table
Model
Add table to DB:
CREATE TABLE cake_contacts (
id INT UNSIGNED AUTO_INCREMENT
PRIMARY KEY,
name VARCHAR(50),
number VARCHAR(50),
created DATETIME DEFAULT NULL,
modified DATETIME DEFAULT NULL
);
Model
Add a script called contact.php to models/
<?php
class Contact extends AppModel
{
var $name = ‘Contact';
}
?>
View
views/contacts/index.thtml
<h1>Contact list</h1>
<p>
<?php echo $html->link('Add Contact',
'contacts/add') ?>
</p>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Number</th>
</tr>
View
views/contacts/index.thtml cntd…
<?php foreach ($contacts as $contact): ?>
<tr>
<td><?php echo $contact['Contact']['id']; ?></td>
<td>
<?php
echo $html->link($contact['Contact'][name'],
"contacts/view/{$contact['Contact']['id']}")?>
[<?php echo $html->link('Edit',
"contacts/edit/{$contact['Contact']['id']}")?>,
<?php echo $html->link('Delete',
"contacts/delete/{$contact['Contact']['id']}",
null, 'Sure?')?>]
</td>
<td><?php echo $contact['Contact']['created']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
View
views/contacts/view.thtml
<?php
class ContactsController extends AppController
{
var $name = 'Contacts';
function index() {
$this->set('contacts', $this->Contact-
>findAll());
}
function view($id) {
$this->Contact->id = $id;
$this->set('data', $this->Contact->read());
}
Controller
/app/controllers/notes_controller.php:
function add() {
if (!empty($this->data['Contact'])) {
if($this->Contact->save($this->data['Contact'])) {
$this->flash('Your contact has been added.',
‘/contacts/');
}
}
}
function delete($id) {
if ($this->Contact->del($id)) {
$this->flash('The contact with id: '.$id.' has been
deleted.', ‘/contacts/');
}
}
Controller
/app/controllers/notes_controller.php: