Zend Framework for IBM i Part II: MVC and ZF Applications
Who is Jeff Olen? Author of bestselling IBM i Programmers Guide to PHP Zend Certified Engineer PHP 5 IBM i developer for 20+ years Co-founder of Olen Business Consulting, Inc. Lead Systems Software Engineer at Vertex Business Services NA
What we will learn What the basic MVC directory structure is The basics of MVC applications How to build ZF applications using DB2 tables How to add bells and whistles to your ZF views.
What you need to know PHP HTML Object Oriented techniques fundamentals Zend Framework Zend Studio - Optional
Review Why use a Framework at all? One entry point for all applications No more maintenance of multiple scripts each with their own relative paths, database connections and authentication, etc Facilitates creating reusable code. What is MVC? MVC is a design pattern. You might think of a design pattern as a skeleton or framework on which your application will be built. The MVC design pattern is based on the idea that there are three components that work together to form a complex application. These components are the Model, the View and the Controller.
The Three Components of MVC: Model, View and Controller What is a Model? Models are the interfaces with the DB. This is where your actual database i/o and validation takes place. There is also where you MAY find some business logic. What is a View? The View contains any code that relates to presentation (i.e. User interface) and presentation logic such as caching or templates.
The Three Components of MVC: Model, View and Controller con t What is a controller? A controller is just that. It controls and launches each of the various activities in an application. For instance in a CRUD application you might have methods in your controller for Add, Update, View and Delete. This is where the business logic resides. Although there may be some business logic in the Model, strictly speaking this is not best practice.
Questions?
ZF Quickstart Application Source available at: http://framework.zend.com We will walk thru the code in the ZF Quickstart application. Go over changes to access DB2 tables. Create our own configuration.ini and access it to determine which database to use. Change the code to be database independent.
MVC directory structure /guestbook /application /configs /controllers /data /forms /layouts /scripts /models /views /scripts /helpers /library /public
.htaccess Apache setup file Location: /guestbook/public/.htaccess SetEnv APPLICATION_ENV development RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L]
Application.ini configuration Location: guestbook/application/configs/application.ini [production] phpsettings.display_startup_errors = 0 phpsettings.display_errors = 0 bootstrap.path = APPLICATION_PATH "/Bootstrap.php" bootstrap.class = "Bootstrap" appnamespace = Application" resources.frontcontroller.controllerdirectory = APPLICATION_PATH "/controllers" resources.frontcontroller.params.displayexceptions = 0 resources.layout.layoutpath = APPLICATION_PATH "/layouts/scripts" resources.view[] = resources.db.adapter = "PDO_SQLITE" resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook.db" [staging : production] [testing : production] phpsettings.display_startup_errors = 1 phpsettings.display_errors = 1 resources.db.adapter = "PDO_SQLITE" resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-testing.db" [development : production] phpsettings.display_startup_errors = 1 phpsettings.display_errors = 1 resources.db.adapter = "PDO_SQLITE" resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-dev.db"
Where do we start? Location: /guestbook/public/index.php
Bootstrap Location: /guestbook/application/bootstrap.php
Questions?
ZF Controller workflow
Brief word about routing ZF Applications use the URL to determine which controller to use to handle a specific request. The default router (object type Zend_Controller_Router_Route_Module) expects the URL in one of the following formats. http://subdomain.domain.com/application/controller/action Or http://subdomain.domain.com/controller/action Examples: http://guestbook.dynalias.net/guestbook/sign Routes to the GuestbookController and the Sign action. http://guestbook.dynalias.net/guestbook Routes to the GuestbookController. No action is specified uses default action (usually index.phtml) http://guestbook.dynalias.net No Controller and no action. Uses default controller (indexcontroller) and default action (index.phtml).
Controllers The Quickstart project has three controllers. IndexController ErrorController GuestbookController
IndexController Location: /guestbook/application/controllers/indexcontroller.php
IndexController default action Location: /guestbook/application/views/scripts/index/index.phtml
Guestbook Layout Location: /guestbook/application/layouts/scripts/layout.phtml
IndexController - output
ErrorController Location: /guestbook/application/controllers/errorcontroller.php
ErrorController - output
GuestbookController Location: /guestbook/application/controllers/guestbookcontroller.php
GuestbookController default action Location: /guestbook/application/views/scripts/guestbook/index.phtml
GuestbookController.php cont d
Questions?
Application Model Guestbook The model in this case is divided into three classes. 1. Application_Model_GuestbookMapper class - Defines the actual mapping between the DB table and the Application_Model_Guestbook class properties. 2. Application_Model_Guestbook class - Defines class properties used to store and manipulate the DB column data. - Contains all GET and SET methods. 3. Application_Model_DbTable_Guestbook class - Calls constructor methods that initialize the metadata for the DB table. - Sets the name and schema (library) of the DB table.
Application_Model_DbTable_Guestbook Location: /guestbook/application/models/dbtable/guestbook.php Calls the following constructor methods: 1. _setupdatabaseadapter() Makes sure a DB adapter has been provided; gets the default adapter from the registry if needed. 2. _setupmetadata() calls describetable() method to load the $_cols array with the columns from the DB table. 3. _setupprimarykey() defaults the primary key columns to those reported by describetable().
Application_Model_Guestbook Location: /guestbook/application/models/guestbook.php
Application_Model_Guestbook cont d Location: /guestbook/application/models/guestbook.php
Application_Model_GuestbookMapper Location: /guestbook/application/models/guestbookmapper.php
Application_Model_GuestbookMapper Location: /guestbook/application/models/guestbookmapper.php
GuestbookController.php cont d
Application_Form_Guestbook Location: /guestbook/application/forms/guestbook.php
Application_Form_Guestbook cont d Location: /guestbook/application/forms/guestbook.php
Application_Form_Guestbook cont d
Questions?
DB2 access changes Create a DB2 table using the SQL below: create table Guestbook ( Id for column GUESTBKID integer not null generated by default as identity(no cache) primary key, email varchar( 100) not null, comment varchar( 50) not null, created timestamp not null default current timestamp );
DB2 access changes cont d Modify the Application_Model_DbTable_Guestbook class IBM i uses all uppercase names so protected $_name = guestbook becomes protected $_name = GUESTBOOK
DB2 access changes cont d Modify the Application_Model_GuestbookMapper class - Again IBM i requires all uppercase names so the field names being retrieved or written to the DB need to be all uppercase.
DB2 access changes cont d Modify the Application_Model_GuestbookMapper class - We also need to uppercase the array indexes in the save method and remove the output for created because we are going default it (per the SQL) to the current timestamp.
DB2 access changes cont d Modify the Application.ini
DB2 access changes cont d THAT S IT!
Further Information Many good books available
Training/Certification Zend offers Framework classes and certification. Starting Fall of 2010 System i Network will be offering a 5 week course on Zend Framework taught by Jeff Olen.
Thank you! Questions? Last chance!