Symphont Php5 Cheat Sheet

ADVERTISEMENT

Open-Source PHP5 MVC Framework
Agile Development
The result of an action execution is a View. A View is the combination of a template
described by a classic PHP file, and a configuration file describing the way this template
VIEW
will fit with other interface elements. It contains some HTML code and some basic PHP
code, usually calls to variables defined in the action and helpers.
TEMPLATES
DECORATOR DESIGN PATTERN
HELPERS
Facilitate the process of writing templates and
NAMING CONVENTIONS
myapp/templates
produce the best possible HTML code in terms
of performance and accessibility.
A template name is made of two parts:
LAYOUT
LAYOUT
mymodule/templates
Three types of helpers are available:
1st part: is related to the action
1)
Standard compulsory helpers
- must be
+
=
2nd part: to the result
TEMPLATE
TEMPLATE
used instead of the corresponding HTML
code because they allow internal symfony
So, an action called
index
that executed
mechanisms (routing, i18n, ...) to work.
successfully is:
2)
Standard optional helpers
, that use less
indexSuccess.php
code than classic HTML for the same
purpose. Using these helpers, you make sure
This is an implicit rule: if the value returned
TEMPLATES LOCATION
that your code will even work after any
by the action is
sfView::SUCCESS
, then the
subsequent change in the file tree structure.
name of the template called will be:
myproject
the
name of the action
concatenated with
3)
Helpers defined specifically for an
apps
Success.php
application
(custom helpers).
myapp
Default helpers - loaded for every app:
With two actions index and list in a product
modules
Helper: defines the use_helper() helpers,
module:
mymodule1
needed for helper inclusion
class productActions extends sfActions{
public function executeIndex(){
templates
defines the basic tag operations
Tag:
...
(templates and partials
links and URL management
Url:
return sfView::SUCCESS;
for module1)
}
head, include, images and js call
Asset:
public function executeList(){
mymodule2
allow inclusion of template fragments
Partial:
...
return sfView::SUCCESS;
manipulation of cached code fragments
Cache:
templates
}
form input
(templates and partials
Form:
}
for module2)
The templates called at the end of the
Loading a non-default helper
execution of each action will be located in
templates
echo use_helper(’HelperName’)
myapp/modules/product/templates/
(layouts for application1 and
and named:
indexSuccess.php
global partials)
Using helpers outside a template
listSuccess.php
sfLoader::loadHelpers($helpers)
ALTERNATE TEMPLATE
DEFAULT GLOBAL LAYOUT
<myproject>/apps/<myapp>/templates/layout.php
An action can set an alternate template:
$this->setTemplate('myCustomTemplate');
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" “http://
The template called is:
myCustomTemplateSuccess.php
<html xmlns=" xml:lang="en" lang="en">
<head>
<?php echo include_http_metas() ?>
VARIABLES
<?php echo include_metas() ?>
Variables called in the templates must be
<?php echo include_title() ?>
either one of the usual shortcuts (see below)
<link rel="shortcut icon" href="/favicon.ico" />
or attributes of the action object defined in
</head>
the related action file.
<body>
E.g.: to define a value for the
$name
variable, the action must contain a line:
<?php echo $sf_data->getRaw('sf_content') ?>
$this->name = 'myvalue';
</body>
</html>
SHORTCUTS
Pre-defined variables or shortcuts:
LAYOUT CONFIGURATION
$sf_context
//the whole context object
$sf_request
//the request
You may have several layouts for your application.
$sf_params
//parameters of the request
The default layout is myproject/apps/myapp/templates/layout.php. Additional layouts must
$sf_user
//the current sfUser object
be added in the same global templates/ directory.
$sf_view
//the calling sfView object
To use a custom layout file, you
Some views don't need any layout at all
E.g.:
can set it in the view.yml file or
(for instance, plain text pages or RSS feeds).
the template code:
in the action.
In that case, set has_layout to false
echo $sf_params->get('total');
LAYOUT DEFINITION IN VIEW.YML
LAYOUT REMOVAL IN VIEW.YML
is equivalent to the following action code:
echo $this->getRequestParameter('total');
indexSuccess:
indexSuccess:
layout: my_layout
has_layout: false
Access the action stack:
$sf_context->getModuleName()
LAYOUT DEFINITION IN ACTION
LAYOUT REMOVAL IN ACTION
//last module called
$this->setLayout('my_layout');
$this->setLayout(false);
$sf_context->getActionName()
//last action called
Ajax actions views have no layout by default.
This cheat-sheet is not an official part of the symfony documentation

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go