zend framework 02 - mvc

18
Zend Framework 2. MVC Tricode Professional Services www.tricode.nl Date: 20-02-2009 Author: Marcel Blok

Upload: tricode

Post on 11-Nov-2014

1.584 views

Category:

Technology


1 download

DESCRIPTION

The Model-View-Controller (MVC) pattern separates the modeling of the domain, the presentation, and the actions based on user input into three separate classes.

TRANSCRIPT

Page 1: Zend framework 02 - mvc

Zend Framework

2. MVC

Tricode Professional Serviceswww.tricode.nl

Date: 20-02-2009Author: Marcel Blok

Page 2: Zend framework 02 - mvc

2

MVC Pattern

Model-view-controller pattern

“The Model-View-Controller(MVC) pattern separates themodeling of the domain, thepresentation, and the

actionsbased on user input into

threeseparate classes”

Controller

ModelView

Page 3: Zend framework 02 - mvc

3

MVC Pattern

The Model

The model manages thebehavior and data of theapplication domain,

respondsto requests for informationabout its state (usually fromthe View), and responds toinstructions to change state(usually from the Controller).

Controller

ModelView

Page 4: Zend framework 02 - mvc

4

MVC Pattern

The View

The Controller interprets theinput from the user,

informingthe Model and/or the View tochange as appropriate.

Controller

ModelView

Page 5: Zend framework 02 - mvc

5

MVC Pattern

The Controller

The Controller interprets theinput from the user,

informingthe Model and/or the View tochange as appropriate.

Controller

ModelView

Page 6: Zend framework 02 - mvc

6

MVC Pattern

Advantages

• High cohesion• Easy to extend• Clear structure• More easy to create automatic tests (like unit-tests)• Etc.

Page 7: Zend framework 02 - mvc

7

ZF MVC

The basic Zend Framework workflow of handling a request is fairly simple.

Request Router preDispatch

Dispatcher

postDispatch

Actions left?

RequestObject

Action Controller

Send Response

Response Object

Page 8: Zend framework 02 - mvc

8

ZF MVC

First the Request is ‘captured’ and a Request Object is made.

The Request Object is just a representation of the Request itself.

By default the HTTP Request Object is used. It holds attributes like the URL and parameters.

Request Router preDispatch

Dispatcher

postDispatch

Actions left?

RequestObject

Action Controller

Send Response

Response Object

Page 9: Zend framework 02 - mvc

9

ZF MVC

Secondly the Router is called.

The Router checks the Request Object, and alters it depending on registered Routes.

By default the Rewrite Router is used. This Router sets the controller, action and parameters from the URL.

Request Router preDispatch

Dispatcher

postDispatch

Actions left?

RequestObject

Action Controller

Send Response

Response Object

Page 10: Zend framework 02 - mvc

10

ZF MVC

After the Routing is done, the Dispatch Loop is started.

The Dispatch Loop is performed until the status of the Request Object is set to ‘dispatched’.

Request Router preDispatch

Dispatcher

postDispatch

Actions left?

RequestObject

Action Controller

Send Response

Response Object

Page 11: Zend framework 02 - mvc

11

ZF MVC

The default Dispatcher is the Standard Dispatcher.

This Dispatcher defines controllers as UpperCamelCase classes that end with Controller, and derfines actions as lowerCamelCase methods ending with Action.

FooController::barAction()

Request Router preDispatch

Dispatcher

postDispatch

Actions left?

RequestObject

Action Controller

Send Response

Response Object

Page 12: Zend framework 02 - mvc

12

ZF MVC

Next, the Action Controller is called.

This is the Controller that you have created yourself.

Like this:

/foo/bar

FooController::barAction()

Request Router preDispatch

Dispatcher

postDispatch

Actions left?

RequestObject

Action Controller

Send Response

Response Object

Page 13: Zend framework 02 - mvc

13

ZF MVC

The Action Controller often implements a View.

As the View is rendered, the output is added to the Response Object.

Request Router preDispatch

Dispatcher

postDispatch

Actions left?

RequestObject

Action Controller

Send Response

Response Object

Page 14: Zend framework 02 - mvc

14

ZF MVC

After the Action Controller has finished the Dispatcher Loop normally finishes.

If the Action Controller resetted the Dispatched status of the Request Object, the Loop continues, and a new Dispatch is triggered.

For instance you can forward to another Action.

Request Router preDispatch

Dispatcher

postDispatch

Actions left?

RequestObject

Action Controller

Send Response

Response Object

Page 15: Zend framework 02 - mvc

15

ZF MVC

At last the Response is sent back.

By default the Response used is the http Response.

Request Router preDispatch

Dispatcher

postDispatch

Actions left?

RequestObject

Action Controller

Send Response

Response Object

Page 16: Zend framework 02 - mvc

16

Auto loading

• PHP5 supports autoloading through the __autoload() function

• From PHP 5.1.2 spl_autoload() is also supported

Page 17: Zend framework 02 - mvc

17

Auto loading

• Zend Framework supports auto loading.

• A file contains 1 class.• The classname resembles the name and

location of the file.

Zend_My_Class

staat in

Zend/My/Class.php

Page 18: Zend framework 02 - mvc

18

Where do I find it?

Projectapplication-- module -- -- controllers-- -- data-- -- layouts-- -- models-- -- views-- -- -- scriptslibrary-- Zendpublictest