SlideShare a Scribd company logo
Wildan Maulana | wildan [at] tobethink.com #2 The Definitive Guide to symfony  Exploring Symfony's Code Doc. v. 0.1 - 14/04/09
MVC Pattern Symfony is based on the  classic web design pattern  known as the MVC  architecture, which consists of  three levels: The Model represents the  information on which the  application operates–its  business logic.
The View renders the  model into a web page  suitable for interaction with  the user.
The Controller responds to  user actions and invokes  changes on the model or  view as appropriate.
MVC Layering
Flat Programming <?php // Connecting, selecting database $link = mysql_connect('localhost', 'myuser', 'mypassword'); mysql_select_db('blog_db', $link); // Performing SQL query $result = mysql_query('SELECT date, title FROM post', $link); ?> <html> <head> <title>List of Posts</title> </head> <body> <h1>List of Posts</h1> <table> <tr><th>Date</th><th>Title</th></tr> <?php // Printing results in HTML while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo &quot;\t<tr>\n&quot;; printf(&quot;\t\t<td> %s </td>\n&quot;, $row['date']); printf(&quot;\t\t<td> %s </td>\n&quot;, $row['title']); echo &quot;\t</tr>\n&quot;; } ?> </table> </body> </html> <?php // Closing connection mysql_close($link); ?> Quick to write, fast to execute,  and impossible to maintain There is no error-checking  (what if the connection to the  database fails?).
HTML and PHP code are mixed,  even interwoven together.
The code is tied to a MySQL  database.
Isolating the Presentation Controller Part : Pure PHP code with all the business logic in it
View Part : HTML code, containing template-like PHP syntax
Controller Part, index.php <?php // Connecting, selecting database $link = mysql_connect('localhost', 'myuser', 'mypassword'); mysql_select_db('blog_db', $link); // Performing SQL query $result = mysql_query('SELECT date, title FROM post', $link); // Filling up the array for the view $posts = array(); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $posts[] = $row; } // Closing connection mysql_close($link); // Requiring the view require('view.php');
The View Part, in view.php <html> <head> <title>List of Posts</title> </head> <body> <h1>List of Posts</h1> <table> <tr><th>Date</th><th>Title</th></tr> <?php foreach ($posts as $post): ?> <tr> <td><?php echo $post['date'] ?></td> <td><?php echo $post['title'] ?></td> </tr> <?php endforeach; ?> </table> </body> </html>
Isolating the Data Manipulation model.php <?php function getAllPosts() { // Connecting, selecting database $link = mysql_connect('localhost', 'myuser', 'mypassword'); mysql_select_db('blog_db', $link); // Performing SQL query $result = mysql_query('SELECT date, title FROM post', $link); // Filling up the array $posts = array(); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $posts[] = $row; } // Closing connection mysql_close($link); return $posts ;  }
The Controller Part, Revised, in index.php <?php // Requiring the model require_once('model.php'); // Retrieving the list of posts $posts = getAllPosts(); // Requiring the view require('view.php');
Layer Separation Beyond MVC Database Abstraction
View Elements
Action and Front Controller
Object Orientation
The Database Abstraction Part of the Model <?php function open_connection($host, $user, $password) { return mysql_connect($host, $user, $password); } function close_connection($link) { mysql_close($link); } function query_database($query, $database, $link) { mysql_select_db($database, $link); return mysql_query($query, $link); } function fetch_results($result) { return mysql_fetch_array($result, MYSQL_ASSOC); }
The Data Access Part of the Model function getAllPosts() { // Connecting to database $link = open_connection('localhost', 'myuser', 'mypassword'); // Performing SQL query $result = query_database('SELECT date, title FROM post', 'blog_db', $link); // Filling up the array $posts = array(); <span class=&quot;kw1&quot;>while ($row = fetch_results($result)) { $posts[] = $row; } // Closing connection close_connection($link); <span class=&quot;kw1&quot;>return $posts; } </span></span> There is no database-engine  dependent  functions can be    found in the data access layer
View Elements Layout  Usually global to the application, or to a group of pages
Template Template only puts in shape the variables made available by the controller
<h1>List of Posts</h1> <table> <tr><th>Date</th><th>Title</th></tr> <?php foreach ($posts as $post): ?> <tr> <td><?php echo $post['date'] ?></td> <td><?php echo $post['title'] ?></td> </tr> <?php endforeach; ?> </table> The Template Part of the View,  in mytemplate.php <?php $title = 'List of Posts'; $content = include('mytemplate.php'); The View Logic Part of the View <html> <head> <title><?php echo $title ?></title> </head> <body> <?php echo $content ?> </body> </html> The Layout Part of the View
Action and Front Controller Controller  Common tasks include  request handling,  security handling, loading  the application  configuration, and similar  chores Front Controller Unique for the whole  application, offers a  unique entry point to  the whole application
Action Contain only the  controller code  specific to one page
Object Orientation All the previous  examples use procedural  programming
Implementing an MVC  architecture in a  language that is not  object-oriented raises  namespace and code- duplication issues, and  the overall code is  difficult to read.
Object orientation allows  developers to deal with  such things as the view  object, the controller  object, and the model  classes, and to transform  all the functions in the  previous examples into  methods. It is a must for  MVC architectures.
Symfony MVC Implementation Model Layer Database abstraction (Propel,  Doctrine, u can choose)
Data access View Layer View
Template

More Related Content

DOC
class 12 board project on database connectivity (java to SQL)
DOCX
IP PROJECT FILE
DOCX
Library management system
PPTX
Ip project
PPTX
Book repository management system
PDF
online library management system
DOCX
IP PROJECT E-GOVERNMENTAL HELPLINE
DOC
FINAL documentation (Rupai Bhadra)
class 12 board project on database connectivity (java to SQL)
IP PROJECT FILE
Library management system
Ip project
Book repository management system
online library management system
IP PROJECT E-GOVERNMENTAL HELPLINE
FINAL documentation (Rupai Bhadra)

What's hot (20)

DOCX
IP Final project 12th
PPTX
SQL Server database project ideas - Top, latest and best project ideas final ...
DOCX
Table of contents
PPTX
Report of Student management system
DOCX
Project 2
PPTX
mini project in c using data structure
PPTX
Library Management System ppt
PPT
CASE TOOLS Questions
PPTX
Library management system
 
PDF
Mini project in java swing
DOCX
Project for Student Result System
PPTX
Library Management System
DOCX
Feedback System in PHP
PPTX
School management system in PHP - Database Project Ideas for Final Year Engin...
ODT
Library Management Project (computer science) class 12
DOCX
Furniture shop management system project report
PDF
DOCX
online blogging system
PDF
Java Software
PPTX
Design and Implementation of Student Profile and Placement management system
IP Final project 12th
SQL Server database project ideas - Top, latest and best project ideas final ...
Table of contents
Report of Student management system
Project 2
mini project in c using data structure
Library Management System ppt
CASE TOOLS Questions
Library management system
 
Mini project in java swing
Project for Student Result System
Library Management System
Feedback System in PHP
School management system in PHP - Database Project Ideas for Final Year Engin...
Library Management Project (computer science) class 12
Furniture shop management system project report
online blogging system
Java Software
Design and Implementation of Student Profile and Placement management system
Ad

Viewers also liked (6)

PPT
Multiple Societal Benefits of Large Scale Restoration
PPS
πανέμορφες φωτογραφίες
PPS
Glyptiki ron mueck - sofia prsd
PDF
κλασικοί πίνακες
PDF
Alantin
PPTX
Oil Pipelines in the Great Lakes, Threats and Solutions-Gosman, 2012
Multiple Societal Benefits of Large Scale Restoration
πανέμορφες φωτογραφίες
Glyptiki ron mueck - sofia prsd
κλασικοί πίνακες
Alantin
Oil Pipelines in the Great Lakes, Threats and Solutions-Gosman, 2012
Ad

Similar to Exploring Symfony's Code (20)

PPT
Php frameworks
PPT
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PDF
Intro To Mvc Development In Php
ODP
Practical catalyst
PPTX
working with PHP & DB's
PDF
Intro to Laravel 4
PPT
Create a web-app with Cgi Appplication
ODP
CodeIgniter PHP MVC Framework
PPTX
WordPress Structure and Best Practices
ODP
Zend Framework 1.9 Setup & Using Zend_Tool
PPT
Php My Sql Security 2007
PPT
Introduction To Code Igniter
PPT
Starting with PHP and Web devepolment
PPTX
Speed up your developments with Symfony2
PPT
P H P Part I I, By Kian
PPS
Simplify your professional web development with symfony
PPTX
Nodejs.meetup
PPTX
Childthemes ottawa-word camp-1919
PDF
Hexagonal architecture in PHP
PPTX
Finding Your Way: Understanding Magento Code
Php frameworks
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
Intro To Mvc Development In Php
Practical catalyst
working with PHP & DB's
Intro to Laravel 4
Create a web-app with Cgi Appplication
CodeIgniter PHP MVC Framework
WordPress Structure and Best Practices
Zend Framework 1.9 Setup & Using Zend_Tool
Php My Sql Security 2007
Introduction To Code Igniter
Starting with PHP and Web devepolment
Speed up your developments with Symfony2
P H P Part I I, By Kian
Simplify your professional web development with symfony
Nodejs.meetup
Childthemes ottawa-word camp-1919
Hexagonal architecture in PHP
Finding Your Way: Understanding Magento Code

More from Wildan Maulana (20)

PDF
Hasil Pendataan Potensi Desa 2018
PDF
Double for Nothing? Experimental Evidence on an Unconditional TeacherSalary I...
PDF
Ketahanan Pangan #1 : Gerakan Sekolah Menanam Melon
PDF
Pengembangan OpenThink SAS 2013-2014
PDF
ICA – AtoM : Retensi Arsip
PDF
OpenThink Labs Workshop : Ketahanan Pangan Skala RT/RW
PDF
OpenThink Labs : Dengar Pendapat Komunitas ciliwung dengan kemen pu dan kemen...
PDF
PostgreSQL BootCamp : Manajemen Master Data dengan SkyTools
PDF
Mensetup Google Apps sebagai IdP jenis openID dan Aplikasi Berbasis CakePHP ...
PDF
Mensetup Google Apps sebagai IdP jenis openID dan Wordpress sebagai Sp
PDF
Konfigurasi simpleSAMLphp dengan Google Apps Sebagai Identity Provider
PDF
Instalasi simpleSAMLphp sebagai Identity Provider (IdP)
PDF
Instalasi dan Konfigurasi simpleSAMLphp
PDF
River Restoration in Asia and Connection Between IWRM and River Restoration
PDF
Optimasi Limpasan Air Limbah Ke Kali Surabaya (Segmen Sepanjang – Jagir) De...
PPT
Penilaian Siswa di Finlandia - Pendidikan Dasar
PDF
Statistik Listrik
PDF
Proyek Al-'Alaq : Electric Bicycles ; History, Characteristics, and Uses
PDF
OpenThink SAS : Interaksi Antara Sekolah, Wali Kelas, Siswa dan Orang Tua
PDF
Menggunakan AlisJK : Equating
Hasil Pendataan Potensi Desa 2018
Double for Nothing? Experimental Evidence on an Unconditional TeacherSalary I...
Ketahanan Pangan #1 : Gerakan Sekolah Menanam Melon
Pengembangan OpenThink SAS 2013-2014
ICA – AtoM : Retensi Arsip
OpenThink Labs Workshop : Ketahanan Pangan Skala RT/RW
OpenThink Labs : Dengar Pendapat Komunitas ciliwung dengan kemen pu dan kemen...
PostgreSQL BootCamp : Manajemen Master Data dengan SkyTools
Mensetup Google Apps sebagai IdP jenis openID dan Aplikasi Berbasis CakePHP ...
Mensetup Google Apps sebagai IdP jenis openID dan Wordpress sebagai Sp
Konfigurasi simpleSAMLphp dengan Google Apps Sebagai Identity Provider
Instalasi simpleSAMLphp sebagai Identity Provider (IdP)
Instalasi dan Konfigurasi simpleSAMLphp
River Restoration in Asia and Connection Between IWRM and River Restoration
Optimasi Limpasan Air Limbah Ke Kali Surabaya (Segmen Sepanjang – Jagir) De...
Penilaian Siswa di Finlandia - Pendidikan Dasar
Statistik Listrik
Proyek Al-'Alaq : Electric Bicycles ; History, Characteristics, and Uses
OpenThink SAS : Interaksi Antara Sekolah, Wali Kelas, Siswa dan Orang Tua
Menggunakan AlisJK : Equating

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Encapsulation theory and applications.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Big Data Technologies - Introduction.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
NewMind AI Monthly Chronicles - July 2025
Electronic commerce courselecture one. Pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Understanding_Digital_Forensics_Presentation.pptx
Encapsulation theory and applications.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
cuic standard and advanced reporting.pdf
Encapsulation_ Review paper, used for researhc scholars
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Machine learning based COVID-19 study performance prediction
Unlocking AI with Model Context Protocol (MCP)
Big Data Technologies - Introduction.pptx
A Presentation on Artificial Intelligence
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
NewMind AI Monthly Chronicles - July 2025

Exploring Symfony's Code

  • 1. Wildan Maulana | wildan [at] tobethink.com #2 The Definitive Guide to symfony Exploring Symfony's Code Doc. v. 0.1 - 14/04/09
  • 2. MVC Pattern Symfony is based on the classic web design pattern known as the MVC architecture, which consists of three levels: The Model represents the information on which the application operates–its business logic.
  • 3. The View renders the model into a web page suitable for interaction with the user.
  • 4. The Controller responds to user actions and invokes changes on the model or view as appropriate.
  • 6. Flat Programming <?php // Connecting, selecting database $link = mysql_connect('localhost', 'myuser', 'mypassword'); mysql_select_db('blog_db', $link); // Performing SQL query $result = mysql_query('SELECT date, title FROM post', $link); ?> <html> <head> <title>List of Posts</title> </head> <body> <h1>List of Posts</h1> <table> <tr><th>Date</th><th>Title</th></tr> <?php // Printing results in HTML while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo &quot;\t<tr>\n&quot;; printf(&quot;\t\t<td> %s </td>\n&quot;, $row['date']); printf(&quot;\t\t<td> %s </td>\n&quot;, $row['title']); echo &quot;\t</tr>\n&quot;; } ?> </table> </body> </html> <?php // Closing connection mysql_close($link); ?> Quick to write, fast to execute, and impossible to maintain There is no error-checking (what if the connection to the database fails?).
  • 7. HTML and PHP code are mixed, even interwoven together.
  • 8. The code is tied to a MySQL database.
  • 9. Isolating the Presentation Controller Part : Pure PHP code with all the business logic in it
  • 10. View Part : HTML code, containing template-like PHP syntax
  • 11. Controller Part, index.php <?php // Connecting, selecting database $link = mysql_connect('localhost', 'myuser', 'mypassword'); mysql_select_db('blog_db', $link); // Performing SQL query $result = mysql_query('SELECT date, title FROM post', $link); // Filling up the array for the view $posts = array(); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $posts[] = $row; } // Closing connection mysql_close($link); // Requiring the view require('view.php');
  • 12. The View Part, in view.php <html> <head> <title>List of Posts</title> </head> <body> <h1>List of Posts</h1> <table> <tr><th>Date</th><th>Title</th></tr> <?php foreach ($posts as $post): ?> <tr> <td><?php echo $post['date'] ?></td> <td><?php echo $post['title'] ?></td> </tr> <?php endforeach; ?> </table> </body> </html>
  • 13. Isolating the Data Manipulation model.php <?php function getAllPosts() { // Connecting, selecting database $link = mysql_connect('localhost', 'myuser', 'mypassword'); mysql_select_db('blog_db', $link); // Performing SQL query $result = mysql_query('SELECT date, title FROM post', $link); // Filling up the array $posts = array(); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $posts[] = $row; } // Closing connection mysql_close($link); return $posts ; }
  • 14. The Controller Part, Revised, in index.php <?php // Requiring the model require_once('model.php'); // Retrieving the list of posts $posts = getAllPosts(); // Requiring the view require('view.php');
  • 15. Layer Separation Beyond MVC Database Abstraction
  • 17. Action and Front Controller
  • 19. The Database Abstraction Part of the Model <?php function open_connection($host, $user, $password) { return mysql_connect($host, $user, $password); } function close_connection($link) { mysql_close($link); } function query_database($query, $database, $link) { mysql_select_db($database, $link); return mysql_query($query, $link); } function fetch_results($result) { return mysql_fetch_array($result, MYSQL_ASSOC); }
  • 20. The Data Access Part of the Model function getAllPosts() { // Connecting to database $link = open_connection('localhost', 'myuser', 'mypassword'); // Performing SQL query $result = query_database('SELECT date, title FROM post', 'blog_db', $link); // Filling up the array $posts = array(); <span class=&quot;kw1&quot;>while ($row = fetch_results($result)) { $posts[] = $row; } // Closing connection close_connection($link); <span class=&quot;kw1&quot;>return $posts; } </span></span> There is no database-engine dependent functions can be found in the data access layer
  • 21. View Elements Layout Usually global to the application, or to a group of pages
  • 22. Template Template only puts in shape the variables made available by the controller
  • 23. <h1>List of Posts</h1> <table> <tr><th>Date</th><th>Title</th></tr> <?php foreach ($posts as $post): ?> <tr> <td><?php echo $post['date'] ?></td> <td><?php echo $post['title'] ?></td> </tr> <?php endforeach; ?> </table> The Template Part of the View, in mytemplate.php <?php $title = 'List of Posts'; $content = include('mytemplate.php'); The View Logic Part of the View <html> <head> <title><?php echo $title ?></title> </head> <body> <?php echo $content ?> </body> </html> The Layout Part of the View
  • 24. Action and Front Controller Controller Common tasks include request handling, security handling, loading the application configuration, and similar chores Front Controller Unique for the whole application, offers a unique entry point to the whole application
  • 25. Action Contain only the controller code specific to one page
  • 26. Object Orientation All the previous examples use procedural programming
  • 27. Implementing an MVC architecture in a language that is not object-oriented raises namespace and code- duplication issues, and the overall code is difficult to read.
  • 28. Object orientation allows developers to deal with such things as the view object, the controller object, and the model classes, and to transform all the functions in the previous examples into methods. It is a must for MVC architectures.
  • 29. Symfony MVC Implementation Model Layer Database abstraction (Propel, Doctrine, u can choose)
  • 30. Data access View Layer View
  • 32. Layout Controller Layer Front Controller
  • 34. Writing MVC code in symfony gives you huge advantages, notably clear code organization, reusability, flexibility, and much more fun. And as a bonus, you have XHTML conformance, debug capabilities, easy configuration, database abstraction, smart URL routing, multiple environments, and many more development tools. <?php class weblogActions extends sfActions { public function executeList() { $this->posts = PostPeer::doSelect(new Criteria()); } } list Action, in myproject/apps/myapp/modules/weblog/actions/actions.class.php <?php slot('title', 'List of Posts') ?> <h1>List of Posts</h1> <table> <tr><th>Date</th><th>Title</th></tr> <?php foreach ($posts as $post): ?> <tr> <td><?php echo $post->getDate() ?></td> <td><?php echo $post->getTitle() ?></td> </tr> <?php endforeach; ?> </table> list Template, in myproject/apps/myapp/modules/weblog/templates/listSuccess.php <html> <head> <title><?php include_slot('title') ?></title> </head> <body> <?php echo $sf_content ?> </body> </html> Layout, in myproject/apps/myapp/templates/layout.php
  • 35. Symfony Core Class The MVC implementation in symfony uses several classes that you will meet quite often : sfController is the controller class. It decodes the request and hands it to the action.
  • 36. sfRequest stores all the request elements (parameters, cookies, headers, and so on).
  • 37. sfResponse contains the response headers and contents. This is the object that will eventually be converted to an HTML response and be sent to the user.
  • 38. The context (retrieved by sfContext::getInstan ce() ) stores a reference to all the core objects and the current configuration; it is accessible from everywhere.
  • 39. Code Organization Project Structure: Applications, Modules, and Actions
  • 40. File Tree Structure All web projects generally share the same types of contents, such as the following: A database, such as MySQL or PostgreSQL
  • 41. Static files (HTML, images, JavaScript files, style sheets, and so on)
  • 42. Files uploaded by the site users and administrators
  • 43. PHP classes and libraries
  • 44. Foreign libraries (third- party scripts)
  • 45. Batch files (scripts to be launched by a command line or via a cron table)
  • 46. Log files (traces written by the application and/ or the server)
  • 48. Root Tree Structure These are the directories found at the root of a symfony project: apps/ frontend/ backend/ cache/ config/ data/ sql/ doc/ lib/ model/ log/ plugins/ test/ bootstrap/ unit/ functional/ web/ css/ images/ js/ uploads/
  • 49. config/ : Holds a hefty set of YAML configuration files. This is where most of the application configuration is, apart from the default parameters that can be found in the framework itself. Note that the default parameters can still be overridden here if needed
  • 50. i18n/ : Contains files used for the internationalization of the application–mostly interface translation files. You can bypass this directory if you choose to use a database for internationalization.
  • 51. lib/ : Contains classes and libraries that are specific to the application.
  • 52. modules/ : Stores all the modules that contain the features of the application.
  • 53. templates/ : Lists the global templates of the application–the ones that are shared by all modules. By default, it contains a layout.php file, which is the main layout in which the module templates are inserted. Application Tree Structure The tree structure of all application directories is the same: apps/ [application name]/ config/ i18n/ lib/ modules/ templates/ layout.php The i18n/, lib/, and modules/ directories are empty for a new application.
  • 54. Module Tree Structure The config/, and lib/ directories are empty for a new module. actions/ : Generally contains a single class file named actions.class.php, in which you can store all the actions of the module. You can also write different actions of a module in separate files.
  • 55. config/ : Can contain custom configuration files with local parameters for the module.
  • 56. lib/ : Stores classes and libraries specific to the module.
  • 57. templates/ : Contains the templates corresponding to the actions of the module. A default template, called indexSuccess.php, is created during module setup. apps/ [application name]/ modules/ [module name]/ actions/ actions.class.php config/ lib/ templates/ indexSuccess.php
  • 58. Web Tree Structure web/ css/ images/ js/ uploads/ css/ : Contains style sheets with a .css extension.
  • 59. images/ : Contains images with a .jpg, .png, or .gif format.
  • 60. js/ : Holds JavaScript files with a .js extension.
  • 61. uploads/ : Can contain the files uploaded by the users. Even though the directory usually contains images, it is distinct from the images directory so that the synchronization of the development and production servers does not affect the uploaded images.
  • 62. Common Instruments A few techniques are used repeatedly in symfony, and you will meet them quite often in your own projects. These include : Parameter Holders
  • 65. Parameter Holders $request->getParameterHolder()->set('foo', 'bar'); echo $request->getParameterHolder()->get('foo'); => 'bar' Or use proxy methods to shorten the code needed for get/set operations : $request->setParameter('foo', 'bar'); echo $request->getParameter('foo'); => 'bar' Symfony parameter holder also support namespaces : $user->setAttribute('foo', 'bar1'); $user->setAttribute('foo', 'bar2', 'my/name/space'); echo $user->getAttribute('foo'); => 'bar1' echo $user->getAttribute('foo', null, 'my/name/space'); => 'bar2'
  • 66. Constants Surprisingly, you will not find any constant in symfony. This is because constants have a major drawback in PHP: you can't change their value once they are defined
  • 67. So symfony uses its own configuration object, called sfConfig , which replaces constants. It provides static methods to access parameters from everywhere. // Instead of PHP constants, define('FOO', 'bar'); echo FOO; // symfony uses the sfConfig object sfConfig::set('foo', 'bar'); echo sfConfig::get('foo');
  • 68. Class Autoloading Classically, when you use a class method or create an object in PHP, you need to include the class definition first. include 'classes/MyClass.php'; $myObject = new MyClass(); By providing an spl_autoload_register() function, symfony makes include statements unnecessary, and you can write directly: $myObject = new MyClass(); Symfony will then look for a MyClass definition in all files ending with php in one of the project's lib/ directories. If the class definition is found, it will be included automatically. NOTE : For better performance, the symfony autoloading scans a list of directories (defined in an internal configuration file) during the first request. It then registers all the classes these directories contain and stores the class/file correspondence in a PHP file as an associative array. That way, future requests don't need to do the directory scan anymore.
  • 69. Summary Using an MVC framework forces you to divide and organize your code according to the framework conventions. Presentation code goes to the view, data manipulation code goes to the model, and the request manipulation logic goes to the controller. It makes the application of the MVC pattern both very helpful and quite restricting.
  • 70. Symfony is an MVC framework written in PHP 5. Its structure is designed to get the best of the MVC pattern, but with great ease of use. Thanks to its versatility and configurability, symfony is suitable for all web application projects.
  • 71. Now that you understand the underlying theory behind symfony, you are almost ready to develop your first application. But before that, you need a symfony installation up and running on your development server.
  • 72. Reference The Definitive Guide to symfony, Fabien Potencier , Apress