Question? Leave a message!

Zend Framework for Enterprise PHP on IBM i

Zend Framework for Enterprise PHP on IBM i 33
Zend Framework for Enterprise PHP on IBM i By Alan Seiden – PHP/i Developer and Consultant Strategic Business Systems, Inc.About me (Alan Seiden) Consultant/developer and mentor specializing in PHP Zend Framework on IBM i • Consultant to IBM’s Redbook: Zend Core for i5/OS • First IBM i developer certified in Zend Framework • Cofounder of first Zend Framework Meetup (New York) • Developed “best web app” (COMMON meeting 2009) 2About Strategic Business Systems, Inc. Partner with IBM since 1982 IBM i hardware, software development, consulting Concentration in food beverage and automotive industries HQ in northern New Jersey Partner with Zend (“the PHP company”) since 2008 PHP’s been our preferred web technology since 2005 In addition to our consulting/development services, we help customers select and purchase Zend’s training and software We represent Zend in the northeastern USA 3What I’ll cover today Evolution of PHP on IBM i In general, and… For each developer, assess where you are and what’s next Zend Framework Why it’s good for you Concepts and components Tips and tricks You will leave here understanding what ZF can do—and that you can do it 4Why PHP on i Powerful, flexible, popular Runs completely on your good old “i” Has backing support of IBM Allows your teams to work together Reuse RPG logic, called from PHP Consolidate departmental solutions (Excel spreadsheets) into your reliable db2 database 5Typical stages of PHP on i Grass roots Prove to yourself how well PHP works on i Pilot project(s) Obtain some buyin from management Enterprise PHP Important project developed with experience 6Grass roots stage 7Grass roots stage Go ahead, kick the tires Download and install Zend Core (free) Create a simple web page reading/writing to db2 Try open source software (such as Mantis/400) Prove it to yourself before “selling” to manager Special tools are optional at this point Even Notepad will do, but Zend Studio is free on IBM i 8Grass roots resources Zend Core: One year of free “silver” support included Online help and information PHP Manual: Zend Core for i5/OS Redbook Knowledge base: Forums (look for i5 forums): 9Pilot Project 10Pilot project: how to pick one Well defined, useful, with modest scope Accesses IBM i resources db2 RPG calls (optional) Could be a new application or extension of an existing one to the web 11Pilot project: goals and resources • Small scale lets you do it right Set standards for future Safe space to get experience • You need these free tools Zend Core, Zend Studio • And one or more of these: Training/Support A PHP framework (e.g. Zend Framework, which is free) Mentoring, coached development • Greatly accelerate progress, save staff time • Especially when starting with a framework Zend Platform (try 60day trial) 12Zend Studio for Eclipse (IDE) •Highlights errors, offers interactive debugging, and speeds creation of Zend Framework projects 13Zend Platform (soon to be Zend Server) Provides caching (fast), debugging and event tracking, integration with Zend Studio (IDE) 14Zend Framework • A starting point for your applications, providing Security features Modular design • A collection of over 70 PHP components to simplify common tasks, including some for: Form creation (and reuse) Logging Database access • A demonstration of PHP 5 best practices • It provides standards and great functionality but will not cramp your style. Your development is not limited in any way 15But what is Enterprise PHP 16Enterprise PHP is: •Grownup development •Think of your mature RPG processes Consistent Reliable •You know how to do that 17Enterprise PHP in detail Design/development cycle Standards Security Focus on easy maintainance Reusable functionality (not just copy and paste) Procedures that ensure quality Deployment Need tools to help support app in live environment Predictable, consistent performance 18Zend Framework An important piece of the enterprise puzzle 19The PHP community is evolving Old question: “what is PHP” Current question: “Which PHP Framework do you use” New question: “How do I integrate “x” framework into a project with Zend Framework” 20Three benefits of a PHP framework 1. Modular design Avoid the tangled mess that comes with growth 2. Best practices You absorb the practices and (we hope) good habits of the framework’s creators. 3. Components Prebuilt functionality Keep up with new technology for you 21Where to get it • Preinstalled with Zend Core /usr/local/Zend/ZendFramework/library php.ini includes it in includepath • Get the latest at Copy into /usr/local/Zend/ZendFramework/library or install elsewhere Can install in your application’s library folder as well Make sure you set include path properly 22Why ZF’s time is right • PHP is being used for critical apps on IBM i • Managers, CIOs, technology architects are taking notice • It’s time for professional practices Standards and consistency Awareness of security Reuse and easy maintenance of code • Leverage your software investments Training and support Doing it “right” • ZF gets you there—“Enterprise PHP”—faster—and keeps you in control 23Why I use it •As I learn what it can do, the less boring code I write I can write less “plumbing” code •Use ZF’s code however you like  Safe for corporate use •It keeps up with trends and APIs Compatibility with diverse database systems, and APIs (authentication, web services, more) 24Zend Framework Community •Contributors include individuals and companies. Companies include: Zend (of course) IBM OmniTI •Technology partners:  Adobe, Google, IBM, Microsoft, nirvanix, StrikeIron 25Here’s why ZF reminds me of the i5 world •Appreciation of standards: naming, parameter lists •The tools you need are already integrated Common components (template system, emailer, etc.) are there for you; no need to research/download/install Upgrades like a “cume tape”—all components upgraded as a well tested unit •ZF support available from Zend Similar to phoning IBM about i5/OS 26ZF’s birth, early years, and maturity on i5 • 2005: PHP Collaboration Project at ZendCon Started as collection of components but coalesced PHP 5, object oriented (OO) from the start Set example of OO design patterns and practices • More on OO later • 20072009: Fast progress July 2007: GA version 1.0 Feb. 2009: version 1.70 with db2/i5 support Oct. 2009: version 1.94; minor releases every couple of weeks • April 2009: ZF/i application won COMMON’s “best web solution” 27COMMON award for best web solution 2009 Allied Beverage Group’s “eBiz” Wine catalog/ordering system Section title slide on IBM i Built in Zend Framework Fulltext catalog search in native db2 28Why eBiz was successful • For Allied, PHP brings people and resources together on the “i” db2 database provides reliable, consistent product catalog and order data No extra servers needed Reusing RPG logic Everyone on the same team PHP is “superglue”: plans afoot to connect other systems, too • In the wine distribution industry, a solid reputation is important For their first highprofile project, asked Strategic (us) to develop the system • Consistency, standards, security, reusability Zend Framework • Speed critical for sales reps in the field (Being compared to green screen speed) Zend Platform 29Allied Beverage eBiz live demo (if time) 30Object orientation (OO) Instant intro: only two slides 31Object Orientation (OO) Here is an incredibly quick summary of OO, which you’ll see used throughout ZF OO Concept Analogy in i5 Example orderNum Property a field in a data structure isOrder() Method function or subprocedure class Order Class Imagine an intelligent data structure containing both data protected orderNum; (properties) and programming logic function isOrder() (methods), which are both called “members” of the class . . . . . . 32OO Syntax •The arrow () lets you access the members (methods and properties) of an object instance controller = thisgetRequest()getControllerName(); •Sometimes you’ll also see the double colon (::), which is similar, but is used when a member is “static” (one per class) echo ZendRegistry::get('user'); •If you can read this notation, you can read ZF code. You will learn to appreciate its simplicity. 33ModelViewController Good structure for your application 34Model – View – Controller (MVC) design pattern •You already know this pattern from RPG/DDS •With green screens, IBM handles it under the covers, so you take it for granted •On the web, you must define your application’s structure more explicitly •Be patient…MVC seems strange at first, but you’ll soon realize that you’ve been here before… 35MVC in detail • Model Reusable classes that access data and business rules Keep SQL and application details in one place • View Templates containing HTML or other output, with small bits of PHP Plunk your HTML into a “view” without worrying about overwriting your mainline PHP code—helps web designers work with business programmers • Controller (action controller) Application flow Connects model and view Don’t confuse with “front controller,” which just initializes the MVC • Next: MVC from an RPG perspective 36RPG Model View Controller (MVC) RPG/Application VIEW (Subroutine) Access/Business 5250 Screen Logic M (DDS) V RPG/Application flow C (Mainline) 37Confession For my first attempt with ZF, I put all my SQL in the controller It gave me a feeling of accomplishment The MVC police did not appear Later, I moved the SQL into a model class Simplified the controller, which was getting complex and hard to understand Made the SQL reusable 38Initialize MVC 39Front controller to action controller 40Front controller routes “friendly” URL •Default routing convention:  Param/value pairs Controller maps are passed to to class name action Action maps to method name Controller1 http action1() action2() request Bootstrap: Front index.php Controller Controller2 action1() action2() 41All requests through index.php in doc root Document root is the only public folder. index.php:  initializes application  instantiates Front Controller 42Apache configuration Most tutorials suggest .htaccess, but I prefer to use the main PASE Apache config file (without proxy): /usr/local/Zend/apache2/conf/httpd.conf Listen 8000 RewriteEngine on NameVirtualHost VirtualHost DocumentRoot /www/ebiz/htdocs/html /VirtualHost Directory /www/ebiz/htdocs/html/ disallow .htaccess, so webserver won’t search for them AllowOverride None funnel all requests to index.php except requests for static resources RewriteEngine On RewriteRule \.(jsicogifjpgpngcsshtml) index.php /Directory 43Front controller bootstrap file: index.php php // minimum bootstrap file (can be many variations) // use explicit, full paths to save the i5 time searching for files paths = array( realpath(dirname(FILE) . '/../library'), realpath(dirname(FILE) . '/../application'), realpath(dirname(FILE) . '/../application/models'), '.' ); setincludepath(implode(PATHSEPARATOR, paths)); // Prepare the front controller frontController = ZendControllerFront::getInstance(); // Dispatch the request using the front controller frontControllerdispatch(); 44Action Controller ZendController 45Action Controller •Controller classes handle groups of request URLs Default: IndexController Organizes and groups functionality One class (extending ZendControllerAction) for each controller •Action methods in each controller class handle requests Default: indexAction() Named like actionAction() •Example: If action is “edit” then method is editAction() 46Controller example 47View ZendView 48View Scripts (templates) PHPbased script templates to present data Should contain only display logic, not business logic Default naming: “myaction.phtml” Helpers Classes and methods that provide reusable view functionality • Examples of builtin view helpers: escape(), formText(), partial(), partialLoop(), headTitle() • Write your own, too Layout 49What View means to you You can plunk HTML right into the view script and replace literals with PHP echo statements: php echo thisproductNum ZF provides smart defaults The thisescape() view helper uses PHP’s htmlentities() function, recommended by most security experts. 50My own view helper: TitleCase.php class ZendViewHelperTitleCase public view; public function titleCase(string = '') return ucwords(strtolower(trim(string))); //(public function titleCase()) public function setView(ZendViewInterface view) Usage: thisview = view; echo thistitleCase(‘mozilla firefox’); // Mozilla Firefox 51Controller (again)…leads to view 52View script automatically rendered 53ZendLayout Gives your site a consistent look while retaining flexibility Twostep view pattern Uses ZendView for rendering Placeholders useful for setting javascript, titles, other variable data 54ZendLayout code 55Model 56Model Model classes are abstract representations of data Can be extended from: •ZendDbTable – For database abstraction •Or any other class that fits your needs •Or build your own own abstract representations of your data Model classes can contain business logic to prepare complex data for presentation I stuff any “weird” code in models so that controllers/views are clean 57Model example: “system busy” flag // model: Busyflag.php class Busyflag protected name = ‘SYSFLAGS'; // oldfashioned “System 36” table // isSiteUp: return true if up, false if down public function isSiteUp() sql = "select BZYFLG from thisname where RECID ='B'"; row = SBSDbhelp::getOneRow(sql); // true if Y, false otherwise. return row'BZYFLG' == 'Y'; //(public function isSiteUp()) //(class Busyflag) // usage (from a preDispatch front controller plugin) busyFlag = new Busyflag(); if (busyFlagisSiteUp()) // Take user to "site down" page. //(if (busyFlagisSiteUp())) 58Components included in ZF Dozens so far…with more on the way 59Library of Zend components Reminder: Zend/Db.php = ZendDb Zend/Db/Table.php = ZendDbTable 60Forms ZendForm component 61ZendForm • Creates the HTML for your data entry forms form = new ZendForm(); formaddElement(‘text’, ‘ordernum‘); formaddElement(‘text’, ‘date‘); • Several ways to output form elements echo form; // (all elements) or echo formordernum; // (just ordernum) or echo formgetElement(‘ordernum’); • The HTML generated by that last echo input type=“text” name=“ordernum” id=“ordernum” 62More complex ZendForm example in MVC // in a model: class MyForm extends ZendForm / Create a text box that checks for nonletter characters and converts text to lower case on submission / formaddElement('text', 'username', array( 'validators' = array( 'alnum', array('regex', false, '/az/i') ), 'required' = true, 'filters' = array('StringToLower'), )); // in a controller: form = new MyForm(); thisview = form // in a view: echo thisform; 63Real life example of ZendForm 64Search results 65Implementation of Product Id field // AdvancedSearchForm class is a model: class AdvancedSearchForm extends ZendForm prodId = new ZendFormElementText("prodid", array('size' = 7, 'maxlength' = 7, 'class' = 'width5')); prodIdsetRequired(false) addFilters(array("StripTags", "StringTrim")) addValidator(new ZendValidateDigits()) setDescription("Partial product ID") setLabel("Code"); thisaddElements(array(prodId)); //(AdvancedSearchForm) 66Database access ZendDb component 67Database access with ZendDb ZendDb can create SQL for you Fewer long, errorprone SQL strings ZendDb offers a lot beyond creating SQL consistent quoting escaping prepared statements (substitutions for “”) profiler 68Database access Several classes give you a good start ZendDbAdapterAbstract: uniform API for many RDBMS’s • ZendDbAdapterDb2 • ZendDbAdapterPdoMysql ZendDbTable • Gateway class for doing queries on a given table ZendDbStatement • Specify SQL for “prepared” statement (securewith marks) …and more 69ZendDbTable ZendDbTable gives you recordlevel access similar to what you may be used to. Insert • productsinsert(array( ‘prodid' = ‘1234567', ‘prodname' = ‘sparkling water’, ); Update Find (like chaining with a key) • results = productsfind(‘1234567’); Delete 70ZendDb techniques for IBM i driverOptions = array('i5lib' = ‘MYLIBRARY'); // Use 'driveroptions' = array('i5naming' = DB2I5NAMINGON)) for liblists config = array( 'host' = 'localhost', 'username' = 'ALAN', 'password' = ‘secret', 'dbname' = 'SBSDB', 'driveroptions' = driverOptions); db = ZendDb::factory('DB2', config); // Using "select" method to select and display records rows = dbselect()from('CUSTOMERS') where('CUSTNO = 0'); // or write your own SQL with parameters sql = 'SELECT FROM CUSTOMERS WHERE CUSTNO and CUSTNO '; rows = dbfetchAll(sql, array(100, 2000)); // either way, output results foreach (rows as row) echo row'CUSTNO' . ' ' . row'CUSTNAME'; 71Config.ini externalizes ZendDb settings ; config.ini dev db.adapter = PDOMYSQL db.params.username = alan db.params.password = secret db.params.dbname = devdb = // in index.php (bootstrap file) config = new ZendConfigIni(realpath(dirname(FILE) . '/../application/config.ini'), 'dev'); db = ZendDb::factory(configdb); 72Working with RPG Wrap i5 Toolkit calls in model classes 73Use models to call RPG from ZF •I always wrap RPG calls in a model class to simplify my code. Here’s why: If the RPG program’s name changes, or we call a different program (e.g. CL instead of RPG), I only need to change the model class, not every place it’s used Implement consistent error handling (e.g. level check) The model bridges the worlds of RPG and PHP •From PHP to RPG, zeropad numbers •From RPG to PHP (return), interpret the RPG’s results – Convert ‘Y’ to ‘true’. Boolean values are well understood by PHP, can be evaluated by if(flag)… 74Example of calling RPG from ZF 75Model hides the details of calling RPG class wer104 public functionconstruct(sequence = 0) // lots of code in here, conversions, erorr handling, etc. . . . parmsIn = array('PWEBID'=sessionKey); . . . I5command . . . thisisValidData = ((returnValues'PRTN' == 'Y') true : false); // be very explicit, true or false final public function isValidData() return thisisValidData; //(class wer104) 76See how simple the controller code is / in controller, we use model ‘wer104’ which wraps/calls RPG / validationCall = new wer104(sequence); if (validationCallisValidData()) // validation failed; redirect to “edit" . . . // otherwise, we passed validation… 77Pagination ZendPaginator 78ZendPaginator •Handles pageattime logic, similar to subfiles, for large lists •Gives you: the right data records Page numbering, back, next, first, last •For data, it’s commonly “fed” an array or db select object If database select, paginator is smart enough to read only the records to be displayed on the page 79ZendPaginator display (The appearance can be fully customized by changing the View and View Partial scripts) 80Example of ZendPaginator code Controller result = dbselect()from("SLEMSTP"); paginator = ZendPaginator::factory(result); // Set parameters for paginator paginatorsetCurrentPageNumber(thisgetParam("page")); // URL must be something like: meaning we are currently on page one, and pass that value into the "setCurrentPageNumber" paginatorsetItemCountPerPage(20); paginatorsetPageRange(10); // Make paginator available in views thisviewpaginator = paginator; View script php if (count(thispaginator)): ul php foreach (thispaginator as item): li= item'LENAME1'; /li php endforeach; /ul php endif; = thispaginationControl(thispaginator, 'Sliding', 'partials/paginationcontrol.phtml'); 81Code to build links for Paginator View Partial (used in View Script on previous slide) NOTE these view helpers: thisurl which build URL links with the prev, next, and other page numbers, and leads back to controller with the page clicked by user. php echo sprintf('Page s of s', thiscurrent, 'xxx'); php if (thispageCount): div class="paginationControl" Previous page link php if (isset(thisprevious)): a href="= thisurl(array('page' = thisprevious)); "lt; Previous/a php else: span class="disabled"lt; Previous/span php endif; Numbered page links php foreach (thispagesInRange as page): php if (page = thiscurrent): a href="= thisurl(array('page' = page)); "= page; /a php else: = page; php endif; php endforeach; Next page link php if (isset(thisnext)): a href="= thisurl(array('page' = thisnext)); "Next gt;/a php else: span class="disabled"Next gt;/span php endif; /div php endif; 82Other components you’ll like 83ZendRegistry •Store global data without polluting the global scope ZendRegistry::set(‘key’, value); ZendRegistry::get(‘key’); Useful for storing sessions, configuration data or any data that could potentially be important on a perrequest basis 84ZendLog • A structured, flexible logging class with several backends  Stream (write to a file)  Firebug  Mail  Db • Example of logging to file system writer = new ZendLogWriterStream('/path/to/logfile'); // Only send entries of warning level and higher. writeraddFilter(ZendLog::WARN); logger = new ZendLog(writer); // later… loggerinfo('Informational message'); loggererr(‘Error message'); // “Error message” 85ZendConfig • OO syntax for reading/writing config files • Internally it’s an array • Multiple backends: INI, XML • Divide sections with , good for dev/prod And hierarchichy with periods after that • db.params.username = xxxx • Many components accept ZendConfig object as well as array Db Forms 86Config + Registry Config.ini: dev db.adapter = PDOMYSQL db.params.username = admin db.params.password = xxxx db.params.dbname = appdb = log.filename = /appdev/logs/app.log config = new ZendConfigIni(realpath(dirname(FILE) . '/../application/config.ini'), 'dev'); registry = ZendRegistry::getInstance(); registryset('config', config); db = ZendDb::factory(configdb); ZendDbTable::setDefaultAdapter(db); registryset('db', db); // create logger; store it in registry logFile = registryget('config')logfilename; writer = new ZendLogWriterStream(logFile); logger = new ZendLog(writer); registryset('logger', logger); 87Caching with ZendCache Frontend Core Output Function File Class Backend Apc File Memcached Sqlite ZendPlatform/Server 88ZendCache: basic code example frontendOptions = array( 'lifetime' = 7200 ); backendOptions = array( 'cachedir' = '../application/cache/' ); cache = ZendCache::factory(‘Output’, ‘File’, frontendOptions, backendOptions); if ((cachestart(‘product list’)) // this content will be held till end() method called echo ‘writing cacheable content…’; cacheend(); // save and output content 89ZendPDF php // combine three TIFF images into a multipage PDF images = array('/images/page1.tif‘,‘/images/page2.tif‘,'/images/page3.tif‘); newFileName = '/pdf/fulldocument.pdf'; pdf = new ZendPdf(); foreach (images as image) // Create new page pdfPage = pdfnewPage(ZendPdfPage::SIZEA4); image = ZendPdfImage::imageWithPath(image); pdfPagedrawImage(image, 10, 10, 400, 500); // add page to pdf pdfpages = pdfPage; //(foreach (images as image)) // Save document as a new file pdfsave(newFileName); 90Client classes for web services • Akismet • Amazon • Audioscrobbler • Delicious • Flickr • Google Data • Nirvanix • ReCaptcha • Simpy • SlideShare • StrikeIron • Technorati • Twitter • Yahoo 91ZendServiceYahoo Search the web with Yahoo Get your application ID from Class uses ZendRestClient under the covers Returns ZendServiceYahooWebResultSet containing instances of ZendServiceYahooWebResult yahoo = new ZendServiceYahoo("YAHOOAPPLICATIONID"); results = yahoowebSearch('IBM PHP', array('results' = ‘10', 'start' = 1)); foreach (results as result) echo 'b' . resultTitle . '/b ' . resultUrl . 'br /'; 92Results from yahoowebSearch 93How to start a ZF project 94Start the right way with Zend Studio for Eclipse • Creates a complete “hello world” application for you Leverage the ZF development team’s best practices 95Resources: online Official information:   Community tutorials and answers:   96Path to ZF •Jump in Have a pilot project in mind Take a ZF training class Get mentoring from someone savvy in both ZF and “i” •Stay connected Join a ZF community, either online or a Meetup in person Subscribe to Zend’s ZF support if it’s a missioncritical app 97Questions Leave feedback at For free PHP/i tips, write to PHP/i blog and articles: Email: LinkedIn: SBS office phone: 18007277260 98
Website URL