======================= - introduction - installation/getting symfony source - using the command line tools - setting up initial project & apps - symfony structure and directories explained - introduction to doctrine and YAML for data model - prototype an application ================================================= - introduction ================================================= What is it? Symfony is a web application framework for PHP5 projects. It aims to speed up the creation and maintenance of web applications, and to replace the repetitive coding tasks by power, control and pleasure. The very small number of prerequisites make symfony easy to install on any configuration; you just need Unix or Windows with a web server and PHP 5 installed. It is compatible with almost every database system. In addition, it has a very small overhead, so the benefits of the framework don't come at the cost of an increase of hosting costs. Using symfony is so natural and easy for people used to PHP and the design patterns of Internet applications that the learning curve is reduced to less than a day. The clean design and code readability will keep your delays short. Developers can apply agile development principles (such as DRY, KISS or the XP philosophy) and focus on applicative logic without losing time to write endless XML configuration files. Symfony is aimed at building robust applications in an enterprise context. This means that you have full control over the configuration: from the directory structure to the foreign libraries, almost everything can be customized. To match your enterprise's development guidelines, symfony is bundled with additional tools helping you to test, debug and document your project. Last but not least, by choosing symfony you get the benefits of an active open-source community. It is entirely free and published under the MIT license. A framework is a re-usable design (in software at least) to solve complex problems, which are encountered many times during development process. Symfony is one of the favorite software development solution in sector. For the ones who try to get one step ahead in finding PHP jobs, MVC pattern based systems can be a great plus. The basic principle of Framework-paradigm is "don't repeat yourself" when solving a problem which has already been solved, at least in a similar case. As a very simple example is database actions. In the first steps, most of PHP coders realize that procedural coding causes a serious problem: "You always repeat some number of similar actions: send query, fetch them, count them...bla bla bla..." and the worst is you do it in another page when the same results of a specific query is needed. Do you really think "copy-pasting" of the previous codes will not begin a problem? What if that table will be normalized? What if the developer group will have new members? Will they read code easily? MVC is short form of "Model-View-Controller". This is a programming pattern, was used in many languages before PHP (e.g. in Java). http://en.wikipedia.org/wiki/Model-view-controller - The Model is the data storage of the system, which is mostly a DB in web applicatins, like MySQL, Oracle, etc. In Symfony or in other PHP-frameworks, model is like a layer, mostly in ORM (object relational model), to help you handle database modeling it as a library of classes. Model of Symfony provides methods to help you access data, modify it, delete it or create a new data, without writing any SQL. (Additionally it keeps an open door for complex SQLs, because sometimes to model a complex SQL is a very hard job using model layer). - The View is the part which renders the response for a required request. This rendering process is managed by controller. So job of View is to be the showcase of the project, it uses HTMLs, JavaScripts, CSSs and many other related browser side technologies. - The Controller works like CPU unit of a processor. Controller gets the request, direct it to the required "action" of the required "module", calls the defined helpers, validation codes, components or multi-language files...If this process seemed disturbing, don't get panic, it's easy to get used to, because it's very "common sense" Where is it? Website: http://www.symfony-project.org/ Documentation: http://www.symfony-project.org/doc/1_0/ Book: http://www.amazon.com/dp/1590597869?tag=symfonyprojec-20&camp=14573&creative=327641&linkCode=as1&creativeASIN=1590597869&adid=0E8EEASTSYS05JS23TXC& When did it start? Symfony was released as an open source project in October, 2005 under the MIT license. Symfony started as a fork of Mojavi3-DEV, a PHP implementation of the Model-View-Controller model, in which an object-relationnal mapping based on Propel was integrated. Helpers for templates, routing, ... inspired by Ruby on Rails, were added in a second time. Eventually, some custom features were integrated - such as cache and scaffolding - that made of symfony more than just another MVC implementation: a unique framework with so many features that it wouldtake months to write the documentation. Symfony uses some other PHP open source projects: * Creole, for database abstraction layer * Pake, for command-line tool * Prado, for i18n support * Spyc, for YAML parsing Some open-source javascript libraries are included in the package: * Prototype, for JavaScript framework * script.aculo.us, for Ajax and visual effects * TinyMCE, for Rich Text Editing * Dynarch.com, for the DHTML Calendar Why symfony? Those who work with symfony don’t just code for pleasure, they also publish websites that work. Even Yahoo! uses symfony for their applications. Not only Yahoo! Bookmarks, but also del.icio.us, the most emblematic web 2.0 service, was completely rewritten to work on the symfony platform. If these guys are willing to spend time and money to rewrite that much code on a framework, it sure shows that the framework is rock solid. But that wouldn’t be fair to mention Yahoo! and not the hundreds (thousands?) of symfony-powered web applications. Thanks for trusting us, and we hope that choosing symfony was a bottom line for your businesses. So symfony has created a new ecosystem, with job offers pouring in, and an increased salary for the developers who know the framework. Downloading and using symfony is free, but it may actually lead you to make a lot of money. ================================================= - installation/getting symfony source ================================================= PEAR pear channel-discover pear.symfony-project.com pear install symfony/symfony SVN checkout: svn co http://svn.symfony-project.com/branches/1.0 externals: svn propedit svn:externals symfony http://svn.symfony-project.com/branches/1.0 SANDBOX download it and unzip it ================================================= - using the command line tools ================================================= CLI ./symfony The command line interface automates common tasks during development such as: - initializing a project, application or module - deploying already developed scaffolding solutions - creating and updating data models - creating and importing test data into the database - generating an admin panel for a data model element - generating a CRUD interface for a data model element - deploying the project to the live environment - handling permissions, log files, cache clearing ================================================= - setting up initial project & apps ================================================= php /srv/symfony/branches/1.0/symfony/data/bin/symfony init-project test ./symfony init-app public ./symfony init-app admin ./symfony init-module public home ./symfony init-module admin home ================================================= - apache configuration ================================================= Alias /sf /path/to/symfony/data/web/sf ================================================= - symfony structure and directories explained ================================================= PROJECT the project is the container for the entire deliverable software contents. the project is global, and should have no internal dependencies on other projects. directories: apps/: applications batch/: batch scripts for command line needs/cron needs cache/: code generation cache output for faster run-time execution data/: generated database initialization files, initial testing data doc/: documentation lib/: classes that are autloaded by the system, and the data model files log/: debug output plugins/: reusable self-contained plugins symfony: the symfony php script shortcut, executable from the command line test/: unit and functional tests web/: the web document root APPLICATIONS: the application is a self-contained interface for the user to interact with, without needing to go between applications. generally, a group of similar modules that together form a desired application. may also be segmented based on separate configuration needs. most common usage: frontend and backend interfaces. directories: apps/xxxxxx/config: application level configuration apps/xxxxxx/i18n: internaltionalization configuration apps/xxxxxx/lib: application specific classes apps/xxxxxx/modules: the modules for this application apps/xxxxxx/templates: the layout template files for this applications views MODULES: a module will usually contain grouped use cases that deal with specific functionality focused around a particular data entity and its nearest relationships. directories: apps/xxxxxx/modules/yyyyyy/actions: the holder for the action controller classes apps/xxxxxx/modules/yyyyyy/config: module specific configuration apps/xxxxxx/modules/yyyyyy/lib: module specific classes apps/xxxxxx/modules/yyyyyy/templates: the template files for the view ACTIONS: an action contains a single use case, or portion of a use case for some specific functionality. file: apps/xxxxxx/modules/yyyyyy/actions/actions.class.php: the holder for the individual controller methods (pages/subpages/ajax views, etc..) within this module ================================================= - plugins ================================================= http://trac.symfony-project.com/wiki/SymfonyPlugins SVN checkout: svn co http://svn.symfony-project.com/plugins/sfDoctrinePlugin/branches/1.0 sfDoctrinePlugin externals: svn propedit svn:externals sfDoctrinePlugin http://svn.symfony-project.com/plugins/sfDoctrinePlugin/branches/1.0 sfDoctrinePlugin svn co http://svn.symfony-project.com/plugins/sfDoctrinePlugin/branches/1.0 sfDoctrinePlugin This symfony plugin allows you to use Doctrine as your ORM instead of Propel in your symfony projects. sfGuardDoctrinePlugin svn co http://svn.symfony-project.com/plugins/sfGuardDoctrinePlugin/branches/symfony-1.0 sfGuardDoctrinePlugin This plugin was originally a port of the propel sfGuardPlugin. It has since changed and new functionality has been added. The plugin now has 6 modules: sfGuardUser, sfGuardGroup, sfGuardPermission, sfGuardAuth, sfGuardRegister, and sfGuardForgotPassword. sfThumbnailPlugin svn co http://svn.symfony-project.com/plugins/sfThumbnailPlugin sfThumbnailPlugin ================================================= - introduction to doctrine and YAML for data model ================================================= YAML Ain't Markup Language YAML is a human friendly data serialization standard for all programming languages. It is indentation (space) sensitive. PHP can be used inside of symfony YAML files. Database config: config/databases.yml Doctrine Doctrine is a PHP ORM (object relational mapper) for PHP 5.2.3+ that sits on top of a powerful PHP DBAL (database abstraction layer). One of its key features is the ability to optionally write database queries in an OO (object oriented) SQL-dialect called DQL inspired by Hibernate's HQL. This provides developers with a powerful alternative to SQL that maintains a maximum of flexibility without requiring needless code duplication. config/doctrine/schema.yml all: connection_name: class: sfDoctrineDatabase param: dsn: mysql://root:@localhost/dbname Schema: config/doctrine/schema.yml --- Profile: actAs: Sluggable: fields: [username] unique: true Timestampable: columns: id: type: integer length: 4 primary: true autoincrement: true sf_guard_user_id: type: integer length: 4 username: type: string length: 255 first_name: type: string length: 255 last_name: type: string length: 255 phone: type: string length: 20 email: type: string length: 255 indexes: username: fields: [username] type: unique relations: sfGuardUser: onDelete: CASCADE foreignAlias: Profile foreignType: one --- Photo: actAs: Sluggable: fields: [title] unique: false Timestampable: columns: id: type: integer length: 4 primary: true autoincrement: true profile_id: type: integer length: 4 file_name: type: string length: 50 title: type: string length: 50 description: type: string indexes: profile_title: fields: [profile_id, title] type: unique relations: Profile: onDelete: CASCADE foreignAlias: Photos --- PhotoTag: columns: id: type: integer length: 4 primary: true autoincrement: true photo_id: type: integer length: 4 tag: type: string length: 50 indexes: photo_tag: fields: [photo_id, tag] type: unique relations: Photo: onDelete: CASCADE foreignAlias: PhotoTags --- PhotoComment: columns: id: type: integer length: 4 primary: true autoincrement: true photo_id: type: integer length: 4 profile_id: type: integer length: 4 body: type: string relations: Photo: onDelete: CASCADE foreignAlias: PhotoComments Profile: onDelete: CASCADE foreignAlias: PhotoComments Test data: data/fixtures/00_fixtures.yml Build: ./symfony doctrine-build-model ./symfony doctrine-build-sql ./symfony doctrine-create-db ./symfony doctrine-insert-sql ./symfony doctrine-load-data - or - ./symfony doctrine-build-all-reload ================================================= - prototype an application ================================================= Let's build a simple photo album Administration ./symfony doctrine-init-admin admin photo Photo apps/admin/modules/photo/config/generator.yml generator: class: sfDoctrineAdminGenerator param: model_class: Photo theme: default list: display: [_thumbnail, title, description, photo_tags_list, created_at] object_actions: _edit: name: Edit photo edit: display: [_thumbnail, file_name, title, description, photo_tags_list] fields: file_name: type: admin_input_file_tag photo_tags_list: name: Tags type: input_tag apps/admin/modules/photo/templates/_thumbnail.yml lib/model/doctrine/Photo.php /** * This class has been auto-generated by the Doctrine ORM Framework */ class Photo extends BasePhoto { public function getThumbnail() { return $this->file_name ? 'thumbnail_' . $this->file_name : ''; } public function setFileName($value) { parent::rawSet('file_name', $value); $this->generateThumbnail($value); } public function generateThumbnail($value) { parent::rawSet('file_name', $value); $uploadDir = sfConfig::get('sf_upload_dir'); $thumbnail = new sfThumbnail(150, 150); $thumbnail->loadFile($uploadDir.'/'.$this->file_name); $thumbnail->save($uploadDir.'/thumbnail_'.$this->file_name, 'image/png'); } public function getPhotoTagsList() { $photo_tags = array(); foreach ($this->PhotoTags as $photo_tag) { $photo_tags[] = $photo_tag->__toString(); } asort($photo_tags); return implode(' ', $photo_tags); } public function setPhotoTagsList($photo_tag_phrase) { $photo_tags = array(); foreach(explode(' ', $photo_tag_phrase) as $photo_tag) { $photo_tags[] = array('name' => $photo_tag); } $this->PhotoTags->synchronizeWithArray($photo_tags); } } lib/model/doctrine/PhotoTag.php /** * This class has been auto-generated by the Doctrine ORM Framework */ class PhotoTag extends BasePhotoTag { public function __toString() { return $this->tag; } } apps/admin/config/settings.yml all: .actions: login_module: sfGuardAuth login_action: signin .settings: enabled_modules: [default, sfGuardAuth, sfGuardUser] apps/admin/lib/myUser.class.php class myUser extends sfGuardSecurityUser { } apps/admin/modules/photo/config/security.yml all: is_secure: on Website apps/public/modules/home/actions/actions.class.php class homeActions extends sfActions { public function executeIndex() { $this->tag = $this->getRequestParameter('tag'); if (strlen($this->tag)) { $this->Photos = Doctrine_Query::create()->select('p.*')->from('Photo p')->leftJoin('p.PhotoTags pt')->where('pt.tag =?', array($this->tag))->execute(); } else { $this->Photos = Doctrine::getTable('Photo')->findAll(); } } public function executePhoto() { $this->Photo = Doctrine::getTable('Photo')->findOneById($this->getRequestParameter('id')); $this->forward404Unless($this->Photo); } } apps/public/modules/home/actions/components.class.php class homeComponents extends sfComponents { public function executePhoto_detail() { if (!$this->Photo) { return sfView::NONE; } } } apps/public/modules/home/templates/_photo_detail.php "" published on , tagged: apps/public/modules/home/templates/indexSuccess.php

My pictures

$Photo)); ?>
apps/public/modules/home/templates/photoSuccess.php

Picture details


$Photo)); ?>

apps/public/config/routing.yml # default rules homepage: url: / param: { module: home, action: index } photo: url: /photo/:id param: { module: home, action: photo } photos_by_tag: url: /photos/:tag param: { module: home, action: photos } default_symfony: url: /symfony/:action/* param: { module: default } default_index: url: /:module param: { action: index } default: url: /:module/:action/*