mvc in symfony

23
MVC IN SYMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years [email protected] http://sayed.justetc.net 1 0 / 8 / 2 0 1 1 s a y e d @ j u s t e t c . n e t 1

Upload: milos

Post on 23-Feb-2016

67 views

Category:

Documents


0 download

DESCRIPTION

MVC in Symfony. Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years [email protected] http://sayed.justetc.net. MVC Design Pattern. Model: defines the business logic the database belongs to this layer - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: MVC in   Symfony

[email protected]

1

MVC IN SYMFONY

Sayed Ahmed

B.Sc. Eng. in Computer Science & EngineeringM. Sc. in Computer ScienceExploring Computing for 14+ [email protected]://sayed.justetc.net

10/8/2011

Page 2: MVC in   Symfony

2

[email protected]

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

Page 3: MVC in   Symfony

3

[email protected]

MVC IN PICTURE 10/8/2011

Page 4: MVC in   Symfony

4

[email protected]

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

Page 5: MVC in   Symfony

5

[email protected]

THE DECORATOR PATTERN 10/8/2011

Page 6: MVC in   Symfony

6

[email protected]

THE DECORATOR PATTERN 10/8/2011

Page 7: MVC in   Symfony

7

[email protected]

DECORATION IN SYMFONY the template is decorated

after the content is rendered by a global template called a layout in symfony

10/8/2011

Page 8: MVC in   Symfony

8

[email protected]

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

Page 9: MVC in   Symfony

9

[email protected]

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

Page 10: MVC in   Symfony

10

[email protected]

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

Page 11: MVC in   Symfony

11

[email protected]

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

Page 12: MVC in   Symfony

12

[email protected]

VIEW LAYER GENERATED BYGENERATE:APP FOR THE TEMPLATE ‘LAYOUT’ 10/8/2011

Page 13: MVC in   Symfony

13

[email protected]

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

Page 14: MVC in   Symfony

14

[email protected]

TEMPLATES indexSuccess and showSuccess sections are

the template names associated with the index and show actions respectively

10/8/2011

Page 15: MVC in   Symfony

15

[email protected]

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

Page 16: MVC in   Symfony

16

[email protected]

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

Page 17: MVC in   Symfony

17

[email protected]

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

Page 18: MVC in   Symfony

18

[email protected]

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

Page 19: MVC in   Symfony

19

[email protected]

THE TEMPLATE By default, the template name associated

with an action is deduced by symfony

10/8/2011

Page 20: MVC in   Symfony

20

[email protected]

THE "FORWARD" METHODS FAMILY $this->forward404If(!$this->job); $this->forward404(); $this->forward('default', '404');

10/8/2011

Page 21: MVC in   Symfony

21

[email protected]

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

Page 22: MVC in   Symfony

22

[email protected]

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

Page 23: MVC in   Symfony

23

[email protected]

REFERENCES http://www.symfony-project.org/jobeet/1_4/Doctrine/

en/05

10/8/2011