SlideShare a Scribd company logo
MVC IN SYMFONY
Sayed Ahmed
B.Sc. Eng. in Computer Science & Engineering
M. Sc. in Computer Science
Exploring Computing for 14+ years
sayed@justetc.net
http://sayed.justetc.net
10/8/2011sayed@justetc.net
1
MVC DESIGN PATTERN
 Model: defines the business logic
 the database belongs to this layer
 Classes and files related to models are stored in
 lib/model/ directory
 View: is what the user interacts with
 a template engine is part of this layer
 the View layer is mainly made of PHP templates
 stored in various templates/ directories
 Controller:
 is a piece of code that calls the Model to get some data that it
passes to the View for rendering to the client.
 all requests are managed by front controllers (index.php and
frontend_dev.php)
 These front controllers delegate the real work to actions.
 these actions are logically grouped into modules
10/8/2011
2
sayed@justetc.net
MVC IN PICTURE
10/8/2011
3
sayed@justetc.net
THE LAYOUT
 The pages generated by Symfony by default has
similar structure such as
 Header
 Template/Body
 Footer
 We can separate the Headers and Footers from
each page
 and place them in a single central place
 and link them (header and footer files) to all pages
 We can use a Design Pattern for the purpose
 decorator design pattern
 http://en.wikipedia.org/wiki/Decorator_pattern
10/8/2011
4
sayed@justetc.net
THE DECORATOR PATTERN
10/8/2011
5
sayed@justetc.net
THE DECORATOR PATTERN
10/8/2011
6
sayed@justetc.net
DECORATION IN SYMFONY
 the template is decorated
 after the content is rendered by a global template
 called a layout in symfony
10/8/2011
7
sayed@justetc.net
DECORATION IN SYMFONY
 The default layout of an application is called layout.php
 and can be found in the apps/frontend/templates/ directory
 This directory contains all the global templates for an
application
 Important:
 You can modify the layout to include all the extra stuff that you
need for all pages including the headers and footers
 You can replace the default symfony layout with your own
layout and create a section for the body/template
 In the example layout file
 http://www.symfony-project.org/jobeet/1_4/Doctrine/en/04
 $sf_content is the main content/template as seen in the pictures of
the page
 $sf_content is the generated HTML based on the current page
 it is defined by the framework itself and contains the HTML
generated by the action.
10/8/2011
8
sayed@justetc.net
IMAGE, CSS, AND JS FILES
 web/images/
 web/css/
 the generate:project task has created three
directories for the project assets: web/images/ for
images, web/~css|CSS~/ for stylesheets, and
web/js/ for JavaScripts
10/8/2011
9
sayed@justetc.net
HELPER
 A helper is a function,
 defined by symfony,
 that can take parameters and returns HTML code.
 Most of the time, helpers are
 time-savers,
 they package code snippets frequently used in templates
 The include_stylesheets() helper generates <link> tags for
stylesheets.
10/8/2011
10
sayed@justetc.net
HELPER, STYLESHEETS, AND VIEW
 The stylesheet files can be included by the
include_stylesheets() function call
 But how does the helper know which stylesheets to
include?
 Using View layer
 View layer generated by
 generate:app
10/8/2011
11
sayed@justetc.net
VIEW LAYER GENERATED BY
GENERATE:APP FOR THE TEMPLATE ‘LAYOUT’
10/8/2011
12
sayed@justetc.net
STYLESHEETS, AND VIEW
 stylesheets: [main.css, jobs.css, job.css]
 symfony prefixes relative paths with /~css|CSS~/.
 Change media
 stylesheets: [main.css, jobs.css, job.css, print: { media: print }]
 The view.yml configuration file can be customized on a
per-module basis
 create a view.yml file in the
apps/frontend/modules/job/config/ directory
 # apps/frontend/modules/job/config/view.yml
 indexSuccess:
 stylesheets: [jobs.css]
 showSuccess:
 stylesheets: [job.css]
10/8/2011
13
sayed@justetc.net
TEMPLATES
 indexSuccess and showSuccess sections are the
template names associated with the index and
show actions respectively
10/8/2011
14
sayed@justetc.net
CONFIGURATION PRINCIPLES IN SYMFONY
 Configuration Principles in symfony
 For many symfony configuration files, the same setting can be
defined at different levels:
 The default configuration is located in the framework
 The global configuration for the project (in config/)
 The local configuration for an application (in apps/APP/config/)
 The local configuration restricted to a module (in
apps/APP/modules/MODULE/config/)
 At runtime, the configuration system merges all the values from
the different files if they exist and caches the result for better
performance.
 use_stylesheet() helper can be used (in the template
file) to include a stylesheet from a template:
 Instead of using view.yml
10/8/2011
15
sayed@justetc.net
LINKING TO JAVASCRIPT FILES
 Symmetrically, the JavaScript configuration is done
via the javascripts entry of the view.yml
configuration file and the use_javascript() helper
defines JavaScript files to include for a template.
 use_javascript() helper defines JavaScript files to
include for a template.
10/8/2011
16
sayed@justetc.net
ACTIONS AND TEMPLATES
 The index action is the Controller part of the page
and the associated template, indexSuccess.php, is
the View part:
10/8/2011
17
sayed@justetc.net
THE ACTION
 Each action is represented by a method of a class
 For the job homepage, the class is jobActions (the
name of the module suffixed by Actions)
 and the method is executeIndex() (execute suffixed
by the name of the action).
 It retrieves all the jobs from the database
10/8/2011
18
sayed@justetc.net
THE TEMPLATE
 By default, the template name associated with an
action is deduced by symfony
10/8/2011
19
sayed@justetc.net
THE "FORWARD" METHODS FAMILY
 $this->forward404If(!$this->job);
 $this->forward404();
 $this->forward('default', '404');
10/8/2011
20
sayed@justetc.net
THE REQUEST AND THE RESPONSE
 The Request
 The sfWebRequest class wraps the $_SERVER, $_COOKIE, $_GET, $_POST,
and $_FILES PHP global arrays:
 Method name -- PHP equivalent
 getMethod() -- $_SERVER['REQUEST_METHOD']
 getUri() $_SERVER['REQUEST_URI']
 getReferer() $_SERVER['HTTP_REFERER']
 getHost() $_SERVER['HTTP_HOST']
 getLanguages() $_SERVER['HTTP_ACCEPT_LANGUAGE']
 getCharsets() $_SERVER['HTTP_ACCEPT_CHARSET']
 isXmlHttpRequest() $_SERVER['X_REQUESTED_WITH'] == 'XMLHttpRequest'
 getHttpHeader() $_SERVER
 getCookie() $_COOKIE
 isSecure() $_SERVER['HTTPS']
 getFiles() $_FILES getGetParameter() $_GET
 getPostParameter() $_POST
 getUrlParameter() $_SERVER['PATH_INFO']
 getRemoteAddress() $_SERVER['REMOTE_ADDR']
10/8/2011
21
sayed@justetc.net
THE RESPONSE
 The Response
 The sfWebResponse class wraps the header() and
setrawcookie() PHP methods:
 Method name PHP equivalent
 setCookie() setrawcookie()
 setStatusCode() header()
 setHttpHeader() header()
 setContentType() header()
 addVaryHttpHeader() header()
 addCacheControlHttpHeader() header()
10/8/2011
22
sayed@justetc.net
REFERENCES
 http://www.symfony-project.org/jobeet/1_4/Doctrine/en/05
10/8/2011
23
sayed@justetc.net

More Related Content

PPTX
Templates, partials and layouts
PPT
Overview of CSharp MVC3 and EF4
PDF
[Laptrinh.vn] lap trinh Spring Framework 3
PPT
Angular Introduction By Surekha Gadkari
PDF
Aspnet mvc tutorial_01_cs
PDF
Building Restful Web App Rapidly in CakePHP
PPTX
Spring mvc
PDF
Yii - Next level PHP Framework von Florian Facker
Templates, partials and layouts
Overview of CSharp MVC3 and EF4
[Laptrinh.vn] lap trinh Spring Framework 3
Angular Introduction By Surekha Gadkari
Aspnet mvc tutorial_01_cs
Building Restful Web App Rapidly in CakePHP
Spring mvc
Yii - Next level PHP Framework von Florian Facker

What's hot (11)

PDF
Angular Best Practices - Perfomatix
PPTX
Angular 5 presentation for beginners
PPT
PDF
User hook implemantation sample example
PDF
Responsive WEB APP using cakePHP
PDF
Murach : How to develop a single-page MVC web
PDF
Rapid Development With CakePHP
PDF
Murach: An introduction to web programming with ASP.NET Core MVC
PPTX
Webinar: PHP and MySQL - Server-side Scripting Language for Web Development
PPTX
Introduction Asp.Net Core, MVC, Docker (Linux), Postman and Swagger
PPTX
Angularjs2 presentation
Angular Best Practices - Perfomatix
Angular 5 presentation for beginners
User hook implemantation sample example
Responsive WEB APP using cakePHP
Murach : How to develop a single-page MVC web
Rapid Development With CakePHP
Murach: An introduction to web programming with ASP.NET Core MVC
Webinar: PHP and MySQL - Server-side Scripting Language for Web Development
Introduction Asp.Net Core, MVC, Docker (Linux), Postman and Swagger
Angularjs2 presentation
Ad

Viewers also liked (19)

PPTX
Be a database professional
PPTX
No audio --and welcome to this presentation
PPT
PPTX
Just will show some dnn administration menu options
PPT
Game balancing
PPTX
Information and communication technology
PPTX
How i made the responsive mobile version of
PPTX
Introduction to c_plus_plus
PPTX
Models in symfony
PPT
Character design
PPTX
Lecture 05 project_time_and_schedule_management
PPTX
Be a database professional
PPTX
Extended bangla first_chapter_computer_and_history_of_computer_short
PPTX
Lecture 03 project_initiation_phase
PPTX
Core mechanics
PPT
User interfaces
PPTX
Whm and cpanel overview hosting control panel overview
PPTX
Linux networking commands
PPTX
Database management system
Be a database professional
No audio --and welcome to this presentation
Just will show some dnn administration menu options
Game balancing
Information and communication technology
How i made the responsive mobile version of
Introduction to c_plus_plus
Models in symfony
Character design
Lecture 05 project_time_and_schedule_management
Be a database professional
Extended bangla first_chapter_computer_and_history_of_computer_short
Lecture 03 project_initiation_phase
Core mechanics
User interfaces
Whm and cpanel overview hosting control panel overview
Linux networking commands
Database management system
Ad

Similar to Mvc in symfony (20)

PDF
Symfony quick tour_2.3
PDF
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
PDF
symfony_from_scratch
PDF
symfony_from_scratch
PPT
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
ODP
Routing
PDF
Symfony Internals
PDF
Symfony2 - WebExpo 2010
PDF
Symfony2 - WebExpo 2010
PDF
Hands-on with the Symfony2 Framework
PDF
Master the New Core of Drupal 8 Now: with Symfony and Silex
PPT
Getting Started with Zend Framework
PDF
BADCamp 2012 -Beginner Best Practices
PDF
6 introduction-php-mvc-cakephp-m6-views-slides
ODP
DrupalEasy: Intro to Theme Development
PDF
Symfony2 - OSIDays 2010
PDF
Introducing symfony
PDF
Symfony for non-techies
PPTX
Creating your own framework on top of Symfony2 Components
KEY
Symfony2 - A Short Introduction
Symfony quick tour_2.3
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
symfony_from_scratch
symfony_from_scratch
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Routing
Symfony Internals
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
Hands-on with the Symfony2 Framework
Master the New Core of Drupal 8 Now: with Symfony and Silex
Getting Started with Zend Framework
BADCamp 2012 -Beginner Best Practices
6 introduction-php-mvc-cakephp-m6-views-slides
DrupalEasy: Intro to Theme Development
Symfony2 - OSIDays 2010
Introducing symfony
Symfony for non-techies
Creating your own framework on top of Symfony2 Components
Symfony2 - A Short Introduction

More from Sayed Ahmed (20)

PDF
Workplace, Data Analytics, and Ethics
PPTX
Python py charm anaconda jupyter installation and basic commands
PPTX
[not edited] Demo on mobile app development using ionic framework
PPTX
Sap hana-ide-overview-nodev
PPTX
Invest wisely
PPTX
Will be an introduction to
PPTX
Web application development using zend framework
PPTX
Web design and_html_part_3
PPTX
Web design and_html_part_2
PPTX
Web design and_html
PPTX
Visual studio ide shortcuts
PPTX
Virtualization
PPT
Unreal
PPTX
Unit tests in_symfony
PPTX
Telerik this is sayed
PPTX
System analysis and_design
PPTX
Symfony 2
PPT
Story telling and_narrative
PPTX
Some skills required to be a computer hardware engineer professional
PPTX
Simple demonstration on
Workplace, Data Analytics, and Ethics
Python py charm anaconda jupyter installation and basic commands
[not edited] Demo on mobile app development using ionic framework
Sap hana-ide-overview-nodev
Invest wisely
Will be an introduction to
Web application development using zend framework
Web design and_html_part_3
Web design and_html_part_2
Web design and_html
Visual studio ide shortcuts
Virtualization
Unreal
Unit tests in_symfony
Telerik this is sayed
System analysis and_design
Symfony 2
Story telling and_narrative
Some skills required to be a computer hardware engineer professional
Simple demonstration on

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Big Data Technologies - Introduction.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
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
Review of recent advances in non-invasive hemoglobin estimation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
KodekX | Application Modernization Development
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Spectroscopy.pptx food analysis technology
cuic standard and advanced reporting.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Big Data Technologies - Introduction.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
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
Review of recent advances in non-invasive hemoglobin estimation
Chapter 3 Spatial Domain Image Processing.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Unlocking AI with Model Context Protocol (MCP)
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Encapsulation theory and applications.pdf
Programs and apps: productivity, graphics, security and other tools
KodekX | Application Modernization Development
Diabetes mellitus diagnosis method based random forest with bat algorithm
Spectroscopy.pptx food analysis technology

Mvc in symfony

  • 1. MVC IN SYMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net http://sayed.justetc.net 10/8/2011sayed@justetc.net 1
  • 2. MVC DESIGN PATTERN  Model: defines the business logic  the database belongs to this layer  Classes and files related to models are stored in  lib/model/ directory  View: is what the user interacts with  a template engine is part of this layer  the View layer is mainly made of PHP templates  stored in various templates/ directories  Controller:  is a piece of code that calls the Model to get some data that it passes to the View for rendering to the client.  all requests are managed by front controllers (index.php and frontend_dev.php)  These front controllers delegate the real work to actions.  these actions are logically grouped into modules 10/8/2011 2 sayed@justetc.net
  • 4. THE LAYOUT  The pages generated by Symfony by default has similar structure such as  Header  Template/Body  Footer  We can separate the Headers and Footers from each page  and place them in a single central place  and link them (header and footer files) to all pages  We can use a Design Pattern for the purpose  decorator design pattern  http://en.wikipedia.org/wiki/Decorator_pattern 10/8/2011 4 sayed@justetc.net
  • 7. DECORATION IN SYMFONY  the template is decorated  after the content is rendered by a global template  called a layout in symfony 10/8/2011 7 sayed@justetc.net
  • 8. DECORATION IN SYMFONY  The default layout of an application is called layout.php  and can be found in the apps/frontend/templates/ directory  This directory contains all the global templates for an application  Important:  You can modify the layout to include all the extra stuff that you need for all pages including the headers and footers  You can replace the default symfony layout with your own layout and create a section for the body/template  In the example layout file  http://www.symfony-project.org/jobeet/1_4/Doctrine/en/04  $sf_content is the main content/template as seen in the pictures of the page  $sf_content is the generated HTML based on the current page  it is defined by the framework itself and contains the HTML generated by the action. 10/8/2011 8 sayed@justetc.net
  • 9. IMAGE, CSS, AND JS FILES  web/images/  web/css/  the generate:project task has created three directories for the project assets: web/images/ for images, web/~css|CSS~/ for stylesheets, and web/js/ for JavaScripts 10/8/2011 9 sayed@justetc.net
  • 10. HELPER  A helper is a function,  defined by symfony,  that can take parameters and returns HTML code.  Most of the time, helpers are  time-savers,  they package code snippets frequently used in templates  The include_stylesheets() helper generates <link> tags for stylesheets. 10/8/2011 10 sayed@justetc.net
  • 11. HELPER, STYLESHEETS, AND VIEW  The stylesheet files can be included by the include_stylesheets() function call  But how does the helper know which stylesheets to include?  Using View layer  View layer generated by  generate:app 10/8/2011 11 sayed@justetc.net
  • 12. VIEW LAYER GENERATED BY GENERATE:APP FOR THE TEMPLATE ‘LAYOUT’ 10/8/2011 12 sayed@justetc.net
  • 13. STYLESHEETS, AND VIEW  stylesheets: [main.css, jobs.css, job.css]  symfony prefixes relative paths with /~css|CSS~/.  Change media  stylesheets: [main.css, jobs.css, job.css, print: { media: print }]  The view.yml configuration file can be customized on a per-module basis  create a view.yml file in the apps/frontend/modules/job/config/ directory  # apps/frontend/modules/job/config/view.yml  indexSuccess:  stylesheets: [jobs.css]  showSuccess:  stylesheets: [job.css] 10/8/2011 13 sayed@justetc.net
  • 14. TEMPLATES  indexSuccess and showSuccess sections are the template names associated with the index and show actions respectively 10/8/2011 14 sayed@justetc.net
  • 15. CONFIGURATION PRINCIPLES IN SYMFONY  Configuration Principles in symfony  For many symfony configuration files, the same setting can be defined at different levels:  The default configuration is located in the framework  The global configuration for the project (in config/)  The local configuration for an application (in apps/APP/config/)  The local configuration restricted to a module (in apps/APP/modules/MODULE/config/)  At runtime, the configuration system merges all the values from the different files if they exist and caches the result for better performance.  use_stylesheet() helper can be used (in the template file) to include a stylesheet from a template:  Instead of using view.yml 10/8/2011 15 sayed@justetc.net
  • 16. LINKING TO JAVASCRIPT FILES  Symmetrically, the JavaScript configuration is done via the javascripts entry of the view.yml configuration file and the use_javascript() helper defines JavaScript files to include for a template.  use_javascript() helper defines JavaScript files to include for a template. 10/8/2011 16 sayed@justetc.net
  • 17. ACTIONS AND TEMPLATES  The index action is the Controller part of the page and the associated template, indexSuccess.php, is the View part: 10/8/2011 17 sayed@justetc.net
  • 18. THE ACTION  Each action is represented by a method of a class  For the job homepage, the class is jobActions (the name of the module suffixed by Actions)  and the method is executeIndex() (execute suffixed by the name of the action).  It retrieves all the jobs from the database 10/8/2011 18 sayed@justetc.net
  • 19. THE TEMPLATE  By default, the template name associated with an action is deduced by symfony 10/8/2011 19 sayed@justetc.net
  • 20. THE "FORWARD" METHODS FAMILY  $this->forward404If(!$this->job);  $this->forward404();  $this->forward('default', '404'); 10/8/2011 20 sayed@justetc.net
  • 21. THE REQUEST AND THE RESPONSE  The Request  The sfWebRequest class wraps the $_SERVER, $_COOKIE, $_GET, $_POST, and $_FILES PHP global arrays:  Method name -- PHP equivalent  getMethod() -- $_SERVER['REQUEST_METHOD']  getUri() $_SERVER['REQUEST_URI']  getReferer() $_SERVER['HTTP_REFERER']  getHost() $_SERVER['HTTP_HOST']  getLanguages() $_SERVER['HTTP_ACCEPT_LANGUAGE']  getCharsets() $_SERVER['HTTP_ACCEPT_CHARSET']  isXmlHttpRequest() $_SERVER['X_REQUESTED_WITH'] == 'XMLHttpRequest'  getHttpHeader() $_SERVER  getCookie() $_COOKIE  isSecure() $_SERVER['HTTPS']  getFiles() $_FILES getGetParameter() $_GET  getPostParameter() $_POST  getUrlParameter() $_SERVER['PATH_INFO']  getRemoteAddress() $_SERVER['REMOTE_ADDR'] 10/8/2011 21 sayed@justetc.net
  • 22. THE RESPONSE  The Response  The sfWebResponse class wraps the header() and setrawcookie() PHP methods:  Method name PHP equivalent  setCookie() setrawcookie()  setStatusCode() header()  setHttpHeader() header()  setContentType() header()  addVaryHttpHeader() header()  addCacheControlHttpHeader() header() 10/8/2011 22 sayed@justetc.net