Tata AIG General Insurance Company - Insurer Innovation Award 2024
Zend framework
1.
2. Intro
• A web application framework written in PHP5
• Loosely coupled set of modules that perform various
Tasks
‣ Database access (Zend_DB)
‣ Google Data API‟s (Zend_Gdata)
• Easy to implement MVC model
3. What is MVC?
• MVC stands for Model-View-Controller
‣ Code is divided into three distinct groups
• Model -- Internal representation of data, interface
to backend storage (i.e. database), and
“business logic”
• View -- Code that represents the application‟s UI
• Controller -- Code that generates output to
populate the view using the model
4. •Download latest zend frame work
•It contains two folders “bin” and “library".
Copy that to wamp/bin/php/=>newfolder say zend_frame.
•Change the include_path for windows in php.ini to
wamp/bin/php/zend_frame/library
Now zend is installed…
to check zend is installed or not
---------------
Take cmd prompt and-> type zf show version
If its displaying the correct version,
Your Zend is functioning
5. Take cmd => go to your local host folder
here its wamp/www/
Type=> zf create project Zendy
projectname
Your project is created….you can check in the www
folder….
Also copy the folder zend in the library to this project`s
library
7. • application/Bootstrap.php
‣ Application bootstrap code
• application/configs
‣ Configuration files
• application/controllers
‣ Backend controller code
• application/models
‣ Code mapping from domain
data to storage data (PHP interface
to DB
for example)
• application/views/scripts
‣ User interface code
• application/configs/application.ini
‣ Main configuration file
• application/controllers/
‣ ErrorController.php
-> Default controller called when an
error occurs
‣ IndexController.php
->Default controller when no
controller is specified
8. You can go to your project by typing localhost/zendy/public on your browser
Your project frontage will be like this:
9. A controller that handles all requests for a Web site.
Zend_Controller_Front
implements a Front Controller
pattern used in Model-View-
Controller (MVC) applications.
Its purpose is to initialize the
request environment, route the
incoming request, and then
dispatch any discovered actions;
it aggregates any responses and
returns them when the process is
complete.
10. The index.php file is the entry point to our application and is used
to create an instance of Zend_Application to initialise our
application and then run it. This file also defines two constants:
APPLICATION_PATH and APPLICATION_ENV which define the path
to the application/ directory and the environment or mode of the
application. The default is set to production in index.php, but you
should set it to development in the .htaccess file by adding this
line: SetEnv APPLICATION_ENV development
12. Adding a New Action
• When forms are submitted, there is some backend
code that processes the input
‣ We will handle this in a new action within the
Indexcontroller
‣ We use the „zf‟ tool to create the relevant code stubs
zf create action actionname controllername
Eg: zf create action about index
• This creates the function aboutAction() in
application/controllers/IndexController.php
class IndexController extends Zend_Controller_Action
{
public function addAction()
{
/* Initialize action controller here */
}
13. EXAMPLE FOR ZEND
Page Controller Action
Home page Index index
Add new album Index add
Edit album Index edit
Delete album Index delete
And the data fields are id(auto increment) ,artist and album
14. Actions
zf create action add Index
zf create action edit Index
zf create action delete Index
The URLs for each action are:
http://localhost/zf-tutorial/public/
IndexController::indexAction()
http://localhost/zf-tutorial/public/index/add
IndexController::addAction()
http://localhost/zf-tutorial/public/index/edit
IndexController::editAction()
http://localhost/zf-tutorial/public/index/delete
IndexController::deleteAction()
class IndexController extends Zend_Controller_Action
{
public function addAction()
{
/* Initialize action controller here*/
}
15. Database configuration
Open application/configs/application.ini and add the following to the
end of the
[production] section (i.e. above the [staging] section):
resources.db.adapter = “PDO_MYSQL”
resources.db.params.host = “localhost”
resources.db.params.username = “root”
resources.db.params.password = “”
resources.db.params.dbname = “zend”
16. Create the database table
CREATE TABLE albums (id int(11) NOT NULL auto_increment,artist
varchar(100) NOT NULL,title varchar(100) NOT NULL,PRIMARY KEY
(id));
Inserting test data
INSERT INTO albums (artist, title) VALUES
('Paolo Nutine', 'Sunny Side Up„),
('Florence + The Machine', 'Lungs'),
('Massive Attack', 'Heligoland'),
('Andre Rieu', 'Forever Vienna'),
('Sade', 'Soldier of Love');
17. The MODEL
Zend Framework provides Zend_Db_Table which implements the
Table Data
Gateway design pattern to allow for interfacing with data in a
database table.
For this tutorial, we are going to create a model that extends
Zend_Db_Table and uses Zend_Db_Table_Row.
zf create db-table Albums albums
<?php
class Application_Model_DbTable_Albums extends
Zend_Db_Table_Abstract
{
protected $_name = 'albums';
}
?>
18.
19.
20. Layouts and views
Zend_Layout allows us to move all the common header, footer and other
code to a layout view script which then includes the specific view code for
the action being executed.
Zf enable layout
Zendy/application/layouts/scripts/layout.phtml
To get the view script for the current action to display, we echo
out the content placeholder using the layout() view helper: echo
$this->layout()->content; which does the work for us.
21. We need to set the doctype for the webpage before we render any view scripts.
As the action view scripts are rendered earlier and may need to know which
doctype is in force. This is especially true for Zend_Form.
To set the doctype we add another line to our application.ini, in the [production]
section:
resources.view.doctype = "XHTML1_STRICT“
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
The doctype() view helper will now output the correct doctype and components
like Zend_Form willgenerate compatible HTML.
22. Styling
Styling is done by making css file in ZENDY/public/css/site.css
And then include that file inside the layout
<?php echo $this->headLink()->prependStylesheet($this->baseUrl().'/css/site.css'); ?>
By using headLink()‟s prependStylesheet() method, we allow for additional,
more specific, CSS files to be added within the controller view scripts which
will be rendered within the <head> section after site.css
23. To display all the dats in the index page…..
zf-tutorial/application/controllers/IndexController.php
...
function indexAction()
{
$albums = new Application_Model_DbTable_Albums();
$this->view->albums = $albums->fetchAll();
}
…
zf-tutorial/application/views/scripts/index/index.phtml
<?php foreach($this->albums as $album) : ?>
<td><?php echo $this->escape($album->title);?></td>
<td><?php echo $this->escape($album->artist);?></td>
<a href="<?php echo $this->url(array('controller'=>'index',
'action'=>'edit', 'id'=>$album->id));?>">Edit</a>
<a href="<?php echo $this->url(array('controller'=>'index',
'action'=>'delete', 'id'=>$album->id));?>">Delete</a>
<?php endforeach; ?>
24.
25. Adding new albums
There are two bits to this part:
• Display a form for user to provide details
• Process the form submission and store to database
zf create form Album
This creates the file Album.php in application/forms
31. This concludes our brief look at building a simple, but
fully functional, MVC application using Zend Framework.
•Zend framework is loosely packed ,but more secure