zend framework 02 - mvc
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
Zend Framework
2. MVC
Tricode Professional Serviceswww.tricode.nl
Date: 20-02-2009Author: Marcel Blok
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
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
4
MVC Pattern
The View
The Controller interprets theinput from the user,
informingthe Model and/or the View tochange as appropriate.
Controller
ModelView
5
MVC Pattern
The Controller
The Controller interprets theinput from the user,
informingthe Model and/or the View tochange as appropriate.
Controller
ModelView
6
MVC Pattern
Advantages
• High cohesion• Easy to extend• Clear structure• More easy to create automatic tests (like unit-tests)• Etc.
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
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
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
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
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
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
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
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
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
16
Auto loading
• PHP5 supports autoloading through the __autoload() function
• From PHP 5.1.2 spl_autoload() is also supported
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
18
Where do I find it?
Projectapplication-- module -- -- controllers-- -- data-- -- layouts-- -- models-- -- views-- -- -- scriptslibrary-- Zendpublictest