• No results found

Lec 13 PHP - V.pptx

N/A
N/A
Protected

Academic year: 2020

Share "Lec 13 PHP - V.pptx"

Copied!
29
0
0

Loading.... (view fulltext now)

Full text

(1)

1

CS428 Web Engineering

Lecture 13

Model View Controller (MVC)

(2)

2

In Today’s Lecture …

• Study object-oriented programming concepts

• MVC Architecture

(3)

• When you have OO code, you can create usable block of code called objects, which you can use in current project and some other project as well.

• As OO code is in modular or components, its really easy to update OO system as oppose to old style system.

• Another advantage of OO based system is its lot easier to have multiple programmers working on same project, because the code is segmented into objects, you can assign different programmers to different objects to work on.

(4)

The OO programming comes into play/shines, when your projects become more and more complex.

You built up your own library when you are using OO code regularly. You can re-use in all of your projects.

OO programming principles are consistent across every OO languages whether it be Java, C# etc.

If you know OOP , you have raised your values as

(5)

• OOP code is more robust, meaning you have to write more code to have same job done.

• OO PHP can be little slower at run time.

(6)
(7)

• In object-oriented programming development, model-view-controller (MVC) is the name of a methodology or design pattern for successfully and efficiently relating the user interface to

underlying data models.

• The MVC pattern separate your code in different components.

(8)

• Every engineered product have three components.

– User Interface

– Mechanism

– Input/Output/Storage

(9)
(10)

User Interface

Mechanism

(11)

Software

User Interface

(View)

Logic/Flow

(Controller)

Storage

(12)

Controller

View Model

User action Update

(13)

Software

User Interface

(View)

Designer

Logic/Flow

(Controller)

Programmer

Storage

(Model)

(14)

User Interface

(Designers)

The view (presentation) is responsible to

display the data provided by the model in a specific format.

• HTML + CSS + JavaScript + jQuery + AJAX

• Bootstrap

(15)

Controller

(Programmer)

The controller handles the model and view

layers to work together. The controller receives a request from the client, invokes the model to perform the requested operations and sends the data to the View. The view formats the data to be presented to the user, in a web application as an html output.

(16)

Model

(DBA)

The model is responsible to manage the data; it stores

and retrieves entities used by an application, usually

from a database, and contains the logic implemented by the application.

• Oracle

• SQL Server

• MySQL

(17)
(18)

Index.php

• <?php

// index.php file

include_once("controller/Controller.php");

$controller = new Controller; $controllerinvoke();

?>

Creating the instance of Controller class

Controller is the first thing, which takes the

(19)

Controller (1/1)

1) The controller is the first thing which takes a request, parses it, initializes and invoke

the model, takes the model response and

sends it to the presentation layer.

2) Its practically the lie between the Model and the View

(20)

Controller (1/2)

• The constructor instantiates a model class and when a request is done, the controller decides

which data is required from the model.

• Then it calls the model class to retrieve the data. After that it calls the corresponding passing the data coming from the model.

• Note that the controller does not know

anything about the database or about how the

(21)

Controller.php

<?php

include_once("model/Model.php");

class Controller { public $model;

public function __construct(){ $this->model = new Model(); }

public function invoke(){ if (!isset($_GET['book'])){

$books = $this->model->getBookList();

include 'view/booklist.php';

} else{

$book = $this->model->getBook($_GET['book']); include 'view/viewbook.php';

} } }

?>

No special book is

requested. We’ll show list of all available books

(22)
(23)

Model and Entity Classes

The Model represents the data and the logic of an

application, what many calls business logic. Usually, it’s responsible for:

storing, deleting, updating the application data. Generally it

includes the database operations, but implementing the same operations invoking external web services or APIs is not an unusual at all.

encapsulating the application logic. This is the layer that should

(24)

Model

• In our example, the model is represented by 2 classes: the “Model” class and “Book” class.

• The “Book” class is an entity class. This class should be exposed to the View layer and

represents the format exported by the model view.

• Note: In a good implementation of the MVC

(25)

Model.php

include_once("model/Book.php");

class Model {

public function getBookList(){

// here goes some hardcoded values to simulate the database

return array(

"Jungle Book" => new Book("Jungle Book", "R. Kipling", "A classic book."),

"Moonwalker" => new Book("Moonwalker", "J. Walker", ""),

"PHP for Dummies" => new Book("PHP for Dummies", "Some Smart Guy", "")

);

}

public function getBook($title){

// we use the previous function to get all the books and then we return the requested one.

// in a real life scenario this will be done through a db select command

$allBooks = $this->getBookList();

return $allBooks[$title];

}

(26)

Book.php

class Book {

public $title;public $author;

public $description;

public function __construct($title, $author, $description)

{

$this->title = $title;

$this->author = $author;

$this->description = $description; }

(27)

View(Presentation)

The view(presentation layer)is responsible for

formatting the data received from the model in a form accessible to the user. The data can come in different formats from the model:

simple objects( sometimes called Value Objects), xml structures, json, …

In our example the view contains only 2 files

(28)

View.php

• <html>

• <head></head>

• <body>

• <?php

• echo 'Title:' . $book->title . '<br/>';

• echo 'Author:' . $book->author . '<br/>';

• echo 'Description:' . $book->description . '<br/>';

• ?>

(29)

Booklist.php

<html><head></head><body><table><tbody><tr><td>Title</td><td>Author</td><td>Description</td></tr></tbody><?php

foreach ($books as $title => $book)

{

References

Related documents

Jak již bylo řečeno, nelze v této verzi spouštět desktopové aplikace, proto také celý systém obsahuje pouze odlehčenou verzi vrstvy Win32, která je navíc zapouzdřená

○ If BP elevated, think primary aldosteronism, Cushing’s, renal artery stenosis, ○ If BP normal, think hypomagnesemia, severe hypoK, Bartter’s, NaHCO3,

Four basic themes emerged from the analysis; social and cyber arrangements within the Dublin Chemsex scene; poly drug use and experiences of drug dependence; drug and sexual

The PROMs questionnaire used in the national programme, contains several elements; the EQ-5D measure, which forms the basis for all individual procedure

Adding keywords to your assets enables you to search and locate your files quickly and accurately, and creating a master keyword list ensures consistency across all users

An analysis of the economic contribution of the software industry examined the effect of software activity on the Lebanese economy by measuring it in terms of output and value

Public awareness campaigns of nonnative fish impacts should target high school educated, canal bank anglers while mercury advisories should be directed at canal bank anglers,

In addition to its internal political problems, Pakistan also faces the issue of al-Qaida and Taliban training camps positioned in its literal back yard, the Federally