Presentation is loading. Please wait.

Presentation is loading. Please wait.

Developing Scalable Web Applications with PHP 5 and Symfony Jeff Jirsa Uniforum Chicago March 27, 2007.

Similar presentations


Presentation on theme: "Developing Scalable Web Applications with PHP 5 and Symfony Jeff Jirsa Uniforum Chicago March 27, 2007."— Presentation transcript:

1 Developing Scalable Web Applications with PHP 5 and Symfony Jeff Jirsa jjirsa@comcast.net Uniforum Chicago March 27, 2007

2 My PHP Web Development Background ● Inherited a Perl/Mason customer sign-up application – Had a prior year's experience developing a Perl application – It sort of worked – Setup of Perl and Mason was a pain – Rewrote the application with PHP 4 – Created functions to encapsulate database access ● Started writing a CMS system a year and a half ago – Started with PHP 5 based object oriented framework – Database encapsulation and basic database classes – Didn't handle security and sessions – Presentation and business logic mixed together – Started using Symfony (www.symfony-project.com) a year ago; version 0.6.3 stable

3 What is Symfony? ● Its a web framework that was influenced by Ruby on Rails ● In 2003, Fabien Potencier from Sensio was looking for advanced PHP 5 tools to develop customer applications ● Symfony was first released in October 2005 based on Mojavi (MVC), Propel (ORM), and Ruby on Rails template helpers ● What makes it compelling? ● Well designed, based on tested components ● Full feature set ● Good documentation ● Easy to develop applications ● A version 1.0.0 tool you can rely on! ● Community

4 Design ● YAML Configuration – YAML: Yet Another Markup Language – Used for all configuration settings, including the database schema – Example ● pager: size: 10 email: corporate: president@acme.com,vp@acme.com,hr@acme.com accounting: bncounter@acme.com logging: onbncounter@acme.com – At run-time the configuration files are converted to PHP

5 Design ● The MVC Pattern – Pattern: a blueprint for design – MVC: Model, View, Controller ● Model: Database and Business logic ● View: Presentation using templates ● Controller: Routes the actions driven by the user

6 Design ● Database Abstraction and Object Relational Mapping (ORM) – The Database abstraction API (Creole) creates a uniform layer for accessing MySQL, Oracle, PostgreSQL, SQL Server, etc. ● Changing database settings are done through configuration ● Developing with one vendor database and switching to another for production is no problem – Object Relational Mapping (Propel) ● Removes (most of) the need for writing SQL statements ● Lets developer deal with record objects ● Related tables are accessed through get() methods ● A standardized file/directory structure separates all aspects of a project into their logical components ● The core framework that handles the YAML, MVC architecture, ORM, etc. are stored in the PEAR module

7 Feature Set ● Symfony is a combination of: – Frameworks, APIs: ● Mojavi (MVC framework), Creole (database abstraction), Propel (Object Relational Mapping), Javascript library (script.aculo.us/Prototype), Javascript calendar (Dynarch), PHPMailer (E-Mail class), Unicode (il8n), Prado (il8n classes) – And tools ● Spyc (YAML parser), Phing (project build utility based on Ant), pake (scafolding generator), and testing framework (lime)

8 Documentation ● On-line documentation sold me on the framework; 200+ pages – Plenty of examples – Developer encouraged to leave comments for corrections and tips ● “The Definitive Guide to symfony” came out recently just after 1.0.0 was released – Replaced the original on-line documentation – Missing some of the information from the original documentation

9 Development Time for a demo!

10 Development ● Step 1: Create the project and the the front-end application – From a command line run: ● md cookbook ● Cd cookbook ● symfony init-project cookbook ● symfony init-app frontend

11 Development ● Step 2: Setup the database – Start the MySQL prompt and run: ● CREATE DATABASE cookbook; ● GRANT ALL PRIVILEGES ON cookbook.* TO cookbook@localhost IDENTIFIED BY 'cookbook';

12 Development ● Step 3: Alter the Propel configuration file – Edit config/propel.ini and add: ● propel.builder.tableType = InnoDB – If you will be using another vendor database with this project, add: ● propel.addVendorInfo = false

13 Development ● Step 4: Create the schema – Edit config/schema.yml and add: ● recipies: id: type: integer required: true autoIncrement: true primaryKey: true title: type: varchar size: 255 required: true instructions: type: longvarchar required: true

14 Development ● Step 5: Create the schema – Edit config/databases.yml and add: ● all: class: sfPropelDatabase param: username: cookbook password: cookbook dev: param: phptype: mysql hostspec: localhost database: cookbook

15 Development ● Step 6: Create the schema – Edit config/databases.yml (cont.) ● prod: param: phptype: mysql hostspec: localhost database: cookbook

16 Development ● Step 7: Generate the model – From a command line run: – symfony propel-build-model

17 Development ● Step 8: Generate the SQL schema – From a command line run: ● symfony propel-build-sql

18 Development ● Step 9: Load the SQL schema – From a MySQL prompt run: ● Source data\sql\lib.model.schema.sql

19 Development ● Step 10: Generate a module – We will need a simple link page for navigation – From a command line run: ● symfony module frontend menu ● edit apps\frontend\modules\menu\actions\actions.class.php – Comment out the forwarding call ● Edit apps\frontend\modules\menu\templates\indexSuccess.php – Add: ● The Symfony Cookbook

20 Development ● Step 11: Generate a CRUD module for Recipes – From a command line run: ● symfony propel-generate-crud frontend recipes Recipes

21 Development ● Step 12: Add the categories table – Edit config\databases.yml and add: ● category_id: type: INTEGER required: true foreignTable: categories foreignReference: id categories: id: type: integer required: true autoIncrement: true primaryKey: true name: type: varchar size: 50 required: true sort_order: type: integer required: true

22 Development ● Step 13: Regenerate the model and SQL schema – From the command line run: ● symfony propel-generate-model ● symfony propel-generate-sql

23 Development ● Step 14: Reload the SQL schema – From a MySQL prompt run: ● Source data\sql\lib.model.schema.sql

24 Development ● Step 15: Load the updated SQL schema – From the command line run: ● symfony propel-generate-crud frontend categories Categories ● symfony propel-generate-crud frontend recipes Recipes

25 Development ● Step 16: Add the validation error messages – From the command line run: ● md apps\frontend\modules\categories\validate ● Edit apps\frontend\modules\categories\validate\update.yml and add: – fields: name: required: msg: Please enter the name of the category. sfStringValidator: min: 5 min_error: "The category name must be at least 5 characters long." max: 50 max_error: "The category name can not be longer than 50 characters."

26 Development ● Step 17: Add the validation error handler – Edit apps\frontend\modules\categories\actions\actions.class.php and add: ● public function handleErrorUpdate() { if( $this->getRequestParameter('id') > 0 ) $this->forward('categories', 'edit'); else $this->forward('categories', 'create'); }

27 Development ● Step 18: Add the validation error display code – Edit apps\frontend\modules\categories\templates\editSuccess.php and add the following before the tagt: ● hasErrors()): ?> Please correct the errors: getErrors() as $name => $error): ?> :

28 Start Using Version 1.0.0! ● Its very easy to get started ● The documentation is very good – Needs to be more searchable on-line ● The core functionality is very solid ● Using a MVC and ORM based framework will make you productive – Symfony lets you focus on business rules, and design ● Comes with a complete toolbox

29 Start Using Version 1.0.0! ● The toolbox – YAML parser (Spyc) – Mojavi (MVC framework) – Creole (database abstraction) – Propel (object relational mapping) – Prado (il8n classes) – Javascript library (script.aculo.us/Prototype) – Javascript calendar (Dynarch) – Mail functions (or class? (PHPMailer) – pake (CLI utility) – testing framework (lime) – Phing (project build utility based on Ant)

30 Community and Resources ● The main web site – http://www.symfony-project.comwww.symfony-project.com ● The forums – http://www.symfony-project.com/forum http://www.symfony-project.com/forum ● The wiki – http://trac.symfony-project.com/trac/wiki http://trac.symfony-project.com/trac/wiki ● IRC – #symfony on freenode.net ● The book – Available at Borders Book Stores

31 Community Questions?


Download ppt "Developing Scalable Web Applications with PHP 5 and Symfony Jeff Jirsa Uniforum Chicago March 27, 2007."

Similar presentations


Ads by Google