my very first zf app part one

57
My Very First… Model View Controller Web Application with the Zend Framework New York City ZF Meetup

Upload: isaaczfoster

Post on 12-Jun-2015

1.237 views

Category:

Documents


0 download

DESCRIPTION

First steps to create a basic app with ZF: using action methods, databases, and forms. From February presentation at ZF-NYC meetup. More to follow in March meetup.

TRANSCRIPT

Page 1: My Very First Zf App   Part One

My Very First…Model View Controller Web Application

with the Zend Framework

New York CityZF Meetup

Page 2: My Very First Zf App   Part One

Part One

• Why use an MVC framework?– Inversion of Control makes life simpler.

• Setting up an environment for use with ZF.• Creating a project with ZF tool.• ZF application basics.

Page 3: My Very First Zf App   Part One

Why Use an MVC Framework?

• Helps to stay DRY (don’t repeat others either)

• Spend time on unique application problems, not plumbing problems.

• Take advantage of combined years of experience.• Things you think of down the line already taken care

of.• Flexible, reliable, well tested code base:– Unit tested.– Community tested.

Page 4: My Very First Zf App   Part One

Inversion of Control

• Framework != Library• A library is a set of tools.• A framework gives you a working structure to

customize by hooking into key points.• The framework invokes your code at the

appropriate time (control is inverted).

Page 5: My Very First Zf App   Part One

Zend Framework MVC Framework + Components Library + CMD Tool

• MVC architecture.• Useful components for everyday tasks:– ACL + Auth– Forms: validation, filtering built in– Web services: Amazon, Twitter, etc

• Command line tool eases set-up tasks.

Page 6: My Very First Zf App   Part One

Zend Framework

• Use at will:– Components can be integrated into an app done

with Lithium, Symfony, Wordpress, Drupal, etc.• Large user community– 500 contributors, 10 million downloads– Corporate backing (+/-)

• Test driven development

Page 7: My Very First Zf App   Part One

Background Info

• MVC frameworks typicallyare made of:– Controllers and action methods• 1 Action Method ≈ 1 Page• Controllers group action methods

– View scripts• Control look and feel of app

– Models• Application data

Page 8: My Very First Zf App   Part One

Simple Example (find the code for a URL)

• In a ZF app, this URL:

• Maps to the “about action method” of the “index controller”

Page 9: My Very First Zf App   Part One

Simple Example

• The “index controller” is a class.• The “about action method” is a method of the

IndexController class.

Page 10: My Very First Zf App   Part One

Simple Example

• About action is called by the framework and renders the “about view script”.

Page 11: My Very First Zf App   Part One

Simple Example

• The results of the view script are output.

Page 12: My Very First Zf App   Part One

Simple Example: Summary

• Framework maps URL to an action method.• You write one method and one view script• Framework calls your code when it’s needed• Framework presents view script to user

Page 13: My Very First Zf App   Part One

Real Project: Tasks

• Want an application to create, manage, and view my tasks.

• Users should be able to register.• Users should be able to create, edit, and

delete individual tasks.• Users should be able to view a list of their

tasks, or any single task.

Page 14: My Very First Zf App   Part One

Tasks: So we’ll need…

• A user component.• A registration component.• A tasks component.• Security to make sure: – Only members can see other users– Only members can do task stuff– Only the owner of a task can do stuff to her tasks– etc

Page 15: My Very First Zf App   Part One

Set Up 1: ZF Command Line Tool

• Download current version of ZF• Folder will contain Zend library, and bin:– Windows: Add bin to your PATH– *nix: Create symbolic link

• Even better, use PEAR installer!

Page 16: My Very First Zf App   Part One

Set Up 2: Zend library to Include Path

• Can add in php.ini• Can add in application’s index.php file

Page 17: My Very First Zf App   Part One

Step 1: Create Project

• Use ZF tool to create project structure.– Must “enter” app directory for further tooling.

Page 18: My Very First Zf App   Part One

Step 1: Create Project

• “public” is the face of your application.– Accessible to civilians– CSS, Javascript, other asset files go here.– index.php is single point of entry.– .htaccess automatically created

• “application” is where most of the app lives.• “library” is for app specific libraries.• “tests” is for unit tests

Page 19: My Very First Zf App   Part One

Step 1: Create Project

• zf create project creates an index controller with a default index action and script

Page 20: My Very First Zf App   Part One

Step 2: Modify Index Action Output

• Want to put something specific to your app on public splash page.

• Need to:– Modify the action method– Modify the view script

• Where are these files in app folder structure?

Page 21: My Very First Zf App   Part One

Step 2: Modify Index Action Output

• One controller class per file, • View scripts housed in views/scripts– Grouped into folder named after “it’s controller”

Page 22: My Very First Zf App   Part One

Step 2: Modify Index Action Output

• Change views/scripts/index/index.phtml from this…

Page 23: My Very First Zf App   Part One

Step 2: Modify Index Action Output

• …to this:

Page 24: My Very First Zf App   Part One

Step 3: Variables in View Scripts

• In ZF, view is an object• View object is in charge of rendering scripts• View scripts execute inside of view object• View scripts have same scope as method of view object

– In other words, in script $this refers to the view object

• Controller has reference to view object• Controller assigns variable to view object for

use in view scripts– Assign as property of view object

Page 25: My Very First Zf App   Part One

Step 3: Variables in View Scripts

• Variables assigned as view object properties in controller:

Page 26: My Very First Zf App   Part One

Step 3: Variables in View Scripts

• Variables used as members of view object inside of view script:

Page 27: My Very First Zf App   Part One

Step 3: Variables in View Scripts

Page 28: My Very First Zf App   Part One

Step 4: Create Another Page

• A Page ≈ An Action Method• Create action methods using ZF tool– Creates method body in controller and view script.– Zf create action actionName controllerItsIn– (Note that you have to have created the controller

using ZF tool to create an action in it using the tool)

Page 29: My Very First Zf App   Part One

Step 4: Create Another Page

• Create an about page:

Page 30: My Very First Zf App   Part One

Step 4: Create Another Page

• Action body and view script magically created!

Page 31: My Very First Zf App   Part One

Step 4: Create Another Page

• Modify view script as desired and presto!

Page 32: My Very First Zf App   Part One

Steps 5 – 8: Users

• Want to be able to register users• Want to be able to store user data• Want to let users manage their data• Want to make sure only users can access user

data

Page 33: My Very First Zf App   Part One

Steps 5 – 8: Users

• So we’ll need:– A way to interact with a user database table

(Zend_Db)– A way for users to interact with information

(Zend_Form)– User pages (UserController)– Log in and access control (Zend_Acl + Zend_Auth)

Page 34: My Very First Zf App   Part One

Steps 5 – 8: Users

• Start with a Users table like so:

Page 35: My Very First Zf App   Part One

Step 5: Create UserController

• We’ll put user related actions in the UserController.

• Use ZF tool to create the controller and actions– Zf create controller controllerName– Will create controller class file, and view scripts

directory

Page 36: My Very First Zf App   Part One

Step 5: Create UserController

Page 37: My Very First Zf App   Part One

Step 5: Create UserController

Page 38: My Very First Zf App   Part One

Step 6: Using a Database

• ZF has adapter based access toDB tables.– Most major DB server’s supported• MySQL, Postgre, Oracle, MSSQL, IBM DB2…

• Query against adapter easier to make changes to backend without ripple effects in consumer code.

• Not Zend_Model

Page 39: My Very First Zf App   Part One

Step 6: Using a Database

• 2 or 3 Steps:– Create DB adapter– Optionally, create table– Use it

Page 40: My Very First Zf App   Part One

Step 6: Using a Database

• We’ll use simple version (query against DB)• For now, DB config in controller init method– Think of init as constructor hook

Page 41: My Very First Zf App   Part One

Step 6: Using a Database

• Method to insert is…insert!

Page 42: My Very First Zf App   Part One

Step 6: Using a Database

• Method to update is…update!

Page 43: My Very First Zf App   Part One

Step 6: Using a Database

• Method to delete is…delete!

Page 44: My Very First Zf App   Part One

Step 6: Using a Database

• Various methods for SELECT statements:– fetchAll– fetchAssoc– fetchNum– fetchObj

Page 45: My Very First Zf App   Part One

Step 6: Using a Database

Page 46: My Very First Zf App   Part One

Step 6: Using a Database

• Methods are similar when using Zend_Db_Table

• Usually configure DB in bootstrap

Page 47: My Very First Zf App   Part One

Step 7: Using Forms

• Users need to be able to register• Use Zend_Form for registration form• Forms and form elements in ZF are objects:– Add elements to form with addElement– Can add validators to elements to ensure data

integrity• EmailAddress validator• Integer validator• Many more

Page 48: My Very First Zf App   Part One

ZF Tool and Forms

Page 49: My Very First Zf App   Part One

Add Elements to Forms

• Element name and type required

Page 50: My Very First Zf App   Part One

Create and Display a Form

• Create in controller, assign to view, use Zend_Form::render() to display.

Page 51: My Very First Zf App   Part One

Handling Form Submission

• Request variables held in request object• Request object accessed in action– Use Zend_Controller_Request::isPost to test for

form submission– Use Zend_Form::isValid() to test data validity– Use Zend_Form::persistData() to save changes• This is what I do, other ways exist

Page 52: My Very First Zf App   Part One

Handling Form Submission

• Action controls flow, form controls data

Page 53: My Very First Zf App   Part One

Valid Data Submission

Page 54: My Very First Zf App   Part One

Invalid Data Submission

Page 55: My Very First Zf App   Part One

Zend Form

• Add and configure form elements• Use Zend_Form::isValid() to test for data

validity• Use Zend_Form::persistData() to commit

changes– Note that step 3 can be done other ways

Page 56: My Very First Zf App   Part One

Step 8 (next time): Access Control

• Zend_Acl– Roles, Resources, Privileges

• Zend_Auth• Action helpers– Basics– Use with access control

• View helpers– Basics– Common helpers

Page 57: My Very First Zf App   Part One

Isaac Foster http://www.linkedin.com/in/[email protected]

New York City area Zend Framework Meetup

http://www.meetup.com/ZendFramework-NYCmetro/

Affiliated with http://www.nyphp.org/

Alan Seiden http://[email protected]: @alanseiden

Thanks for attending “Your First Zend Framework Project”

presented on Feb. 22, 2011 by

Sign up to hear about all our ZF meetups at http://www.meetup.com/ZendFramework-NYCmetro/