Wildan Maulana | wildan [at] tobethink.com #2 The Definitive Guide to symfony  Exploring Symfony's Code Doc. v. 0.1 - 14/0...
MVC Pattern <ul><li>Symfony is based on the  classic web design pattern  known as the MVC  architecture, which consists of...
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. </li></ul></ul>
MVC Layering
Flat Programming <?php // Connecting, selecting database $link = mysql_connect('localhost', 'myuser', 'mypassword'); mysql...
HTML and PHP code are mixed,  even interwoven together.
The code is tied to a MySQL  database. </li></ul>
Isolating the Presentation <ul><li>Controller Part : Pure PHP code with all the business logic in it
View Part : HTML code, containing template-like PHP syntax </li></ul>
Controller Part, index.php <?php // Connecting, selecting database $link = mysql_connect('localhost', 'myuser', 'mypasswor...
The View Part, in view.php <html> <head> <title>List of Posts</title> </head> <body> <h1>List of Posts</h1> <table> <tr><t...
Isolating the Data Manipulation model.php <?php function getAllPosts() { // Connecting, selecting database $link = mysql_c...
The Controller Part, Revised, in index.php <?php // Requiring the model require_once('model.php'); // Retrieving the list ...
Layer Separation Beyond MVC <ul><li>Database Abstraction
View Elements
Action and Front Controller
Object Orientation </li></ul>
The Database Abstraction Part of the Model <?php function open_connection($host, $user, $password) { return mysql_connect(...
The Data Access Part of the Model function getAllPosts() { // Connecting to database $link = open_connection('localhost', ...
View Elements <ul><li>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 </li></ul>
<h1>List of Posts</h1> <table> <tr><th>Date</th><th>Title</th></tr> <?php foreach ($posts as $post): ?> <tr> <td><?php ech...
Action and Front Controller <ul><li>Controller  Common tasks include  request handling,  security handling, loading  the a...
Action Contain only the  controller code  specific to one page </li></ul></ul>
Object Orientation <ul><li>All the previous  examples use procedural  programming
Implementing an MVC  architecture in a  language that is not  object-oriented raises  namespace and code- duplication issu...
Object orientation allows  developers to deal with  such things as the view  object, the controller  object, and the model...
Symfony MVC Implementation <ul><li>Model Layer </li><ul><li>Database abstraction (Propel,  Doctrine, u can choose)
Data access </li></ul><li>View Layer </li><ul><li>View
Template
Upcoming SlideShare
Loading in …5
×

Exploring Symfony's Code

3,801 views
3,762 views

Published on

Excerpt from : The Definitive Guide to symfony

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,801
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
129
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Exploring Symfony's Code

  1. 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. 2. MVC Pattern <ul><li>Symfony is based on the classic web design pattern known as the MVC architecture, which consists of three levels: </li><ul><li>The Model represents the information on which the application operates–its business logic.
  3. 3. The View renders the model into a web page suitable for interaction with the user.
  4. 4. The Controller responds to user actions and invokes changes on the model or view as appropriate. </li></ul></ul>
  5. 5. MVC Layering
  6. 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; <tr> &quot;; printf(&quot; <td> %s </td> &quot;, $row['date']); printf(&quot; <td> %s </td> &quot;, $row['title']); echo &quot; </tr> &quot;; } ?> </table> </body> </html> <?php // Closing connection mysql_close($link); ?> Quick to write, fast to execute, and impossible to maintain <ul><li>There is no error-checking (what if the connection to the database fails?).
  7. 7. HTML and PHP code are mixed, even interwoven together.
  8. 8. The code is tied to a MySQL database. </li></ul>
  9. 9. Isolating the Presentation <ul><li>Controller Part : Pure PHP code with all the business logic in it
  10. 10. View Part : HTML code, containing template-like PHP syntax </li></ul>
  11. 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. 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. 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. 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. 15. Layer Separation Beyond MVC <ul><li>Database Abstraction
  16. 16. View Elements
  17. 17. Action and Front Controller
  18. 18. Object Orientation </li></ul>
  19. 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. 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. 21. View Elements <ul><li>Layout Usually global to the application, or to a group of pages
  22. 22. Template Template only puts in shape the variables made available by the controller </li></ul>
  23. 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. 24. Action and Front Controller <ul><li>Controller Common tasks include request handling, security handling, loading the application configuration, and similar chores </li><ul><li>Front Controller Unique for the whole application, offers a unique entry point to the whole application
  25. 25. Action Contain only the controller code specific to one page </li></ul></ul>
  26. 26. Object Orientation <ul><li>All the previous examples use procedural programming
  27. 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. 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. </li></ul>
  29. 29. Symfony MVC Implementation <ul><li>Model Layer </li><ul><li>Database abstraction (Propel, Doctrine, u can choose)
  30. 30. Data access </li></ul><li>View Layer </li><ul><li>View
  31. 31. Template
  32. 32. Layout </li></ul><li>Controller Layer </li><ul><li>Front Controller
  33. 33. Action </li></ul></ul>
  34. 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. 35. Symfony Core Class <ul><li>The MVC implementation in symfony uses several classes that you will meet quite often : </li><ul><li>sfController is the controller class. It decodes the request and hands it to the action.
  36. 36. sfRequest stores all the request elements (parameters, cookies, headers, and so on).
  37. 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. 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. </li></ul></ul>
  39. 39. Code Organization <ul><li>Project Structure: Applications, Modules, and Actions </li></ul>
  40. 40. File Tree Structure <ul><li>All web projects generally share the same types of contents, such as the following: </li><ul><li>A database, such as MySQL or PostgreSQL
  41. 41. Static files (HTML, images, JavaScript files, style sheets, and so on)
  42. 42. Files uploaded by the site users and administrators
  43. 43. PHP classes and libraries
  44. 44. Foreign libraries (third- party scripts)
  45. 45. Batch files (scripts to be launched by a command line or via a cron table)
  46. 46. Log files (traces written by the application and/ or the server)
  47. 47. Configuration files </li></ul></ul>
  48. 48. Root Tree Structure <ul><li>These are the directories found at the root of a symfony project: </li></ul>apps/ frontend/ backend/ cache/ config/ data/ sql/ doc/ lib/ model/ log/ plugins/ test/ bootstrap/ unit/ functional/ web/ css/ images/ js/ uploads/
  49. 49. <ul><li>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. 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. 51. lib/ : Contains classes and libraries that are specific to the application.
  52. 52. modules/ : Stores all the modules that contain the features of the application.
  53. 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. </li></ul>Application Tree Structure <ul><li>The tree structure of all application directories is the same: </li></ul>apps/ [application name]/ config/ i18n/ lib/ modules/ templates/ layout.php The i18n/, lib/, and modules/ directories are empty for a new application.
  54. 54. Module Tree Structure The config/, and lib/ directories are empty for a new module. <ul><li>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. 55. config/ : Can contain custom configuration files with local parameters for the module.
  56. 56. lib/ : Stores classes and libraries specific to the module.
  57. 57. templates/ : Contains the templates corresponding to the actions of the module. A default template, called indexSuccess.php, is created during module setup. </li></ul>apps/ [application name]/ modules/ [module name]/ actions/ actions.class.php config/ lib/ templates/ indexSuccess.php
  58. 58. Web Tree Structure web/ css/ images/ js/ uploads/ <ul><li>css/ : Contains style sheets with a .css extension.
  59. 59. images/ : Contains images with a .jpg, .png, or .gif format.
  60. 60. js/ : Holds JavaScript files with a .js extension.
  61. 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. </li></ul>
  62. 62. Common Instruments <ul>A few techniques are used repeatedly in symfony, and you will meet them quite often in your own projects. These include : <ul><li>Parameter Holders
  63. 63. Constants
  64. 64. Class Autoloading </li></ul></ul>
  65. 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. 66. Constants <ul><li>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. 67. So symfony uses its own configuration object, called sfConfig , which replaces constants. It provides static methods to access parameters from everywhere. </li></ul>// Instead of PHP constants, define('FOO', 'bar'); echo FOO; // symfony uses the sfConfig object sfConfig::set('foo', 'bar'); echo sfConfig::get('foo');
  68. 68. Class Autoloading <ul><li>Classically, when you use a class method or create an object in PHP, you need to include the class definition first. </li></ul>include 'classes/MyClass.php'; $myObject = new MyClass(); <ul><li>By providing an spl_autoload_register() function, symfony makes include statements unnecessary, and you can write directly: </li></ul>$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. 69. Summary <ul><li>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. 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. 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. </li></ul>
  72. 72. Reference <ul><li>The Definitive Guide to symfony, Fabien Potencier , Apress </li></ul>

×