Chapter 5 - Configuring Symfony
Chapter 8 - Inside The Model Layer
Chapter 9 - Links And The Routing System
Key Notes
- YAML
- Project, Application, and Module levels
- Environment.
- Available from the PHP code of your application.
- PHP code in YAML files
Usage
- Security
- Validation
- Application constants
- ...
Key notes... cont.
Processed by
Configuration system and not the application
YAML Syntax
# Never use tabs
all
:
-> mail:
-> -> webmaster: webmaster
@example
.com
# Use blanks instead
all
:
mail:
webmaster
: webmaster
@example
.com
Comments
# This is a comment line
mail:
webmaster
: webmaster
@example
.com
contact
: contact
@example
.com
admin
: admin
@example
.com
# extra spaces allow nice alignment of values
Strings
error1: This field is compulsory
error2: ' This field is compulsory '
error3: 'Don''t leave this field blank' # Single quotes must be doubled
error4: 'Enter a # symbol to define an extension number'
i18n: 'no' # if we left off the quotes here, a boolean false would be returned
Multiple-line strings:
# Folded style, introduced by >
# Each line break is folded to a space
# Makes YAML more readable
accomplishment: >
Mark set a major league
home run record in 1998.
# Literal style, introduced by |
# All line breaks count
# Indentation doesn't appear in the resulting string
stats: |
65 Home Runs
0.278 Batting Average
Arrays
# Shorthand syntax for arrays
players: [ Mark McGwire, Sammy Sosa, Ken Griffey ]
# Expanded syntax for arrays
players:
- Mark McGwire
- Sammy Sosa
- Ken Griffey
Associative Arrays
# Correct shorthand syntax for associative arrays
mail: { webmaster
: webmaster
@example
.com
, contact
: contact
@example
.com
}
# Expanded syntax for associative arrays
mail:
webmaster
: webmaster
@example
.com
contact
: contact
@example
.com
Boolean
true_values: [ on, true, +, yes, y ]
false_values: [ off, false, -, no, n ]
Category
all
:
.general
:
tax
: 19.6
mail:
webmaster
: webmaster
@example
.com
equivalent to
all
:
tax
: 19.6
mail:
webmaster
: webmaster
@example
.com
PHP in YAML
all:
translation:
format:
<?php echo (sfConfig
::get('sf_i18n') == true ?
'xliff' : 'none')."\n" ?>
Example
house:
family:
name: Doe
parents: [John, Jane]
children: [Paul, Mark, Simone]
address:
number: 34
street: Main Street
city: Nowheretown
zipcode: 12345
$test = sfYaml
::load('/path/to/test.yml');
print_r($test);
Array(
[house
] => Array(
[family
] => Array(
[name
] => Doe
[parents
] => Array(
[0] => John
[1] => Jane
)
[children
] => Array(
[0] => Paul
[1] => Mark
[2] => Simone
)
)
[address
] => Array(
[number
] => 34
[street
] => Main Street
[city
] => Nowheretown
[zipcode
] => 12345
)
)
) Common Config Files
- ProjectConfiguration.class.php
- databases.yml
- properties.ini
- schema.yml
- propel.ini
Very first Configuration
<?php
require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration
::getApplicationConfiguration('frontend', 'prod', false);
sfContext
::createInstance($configuration)->dispatch();
useful methods:
$configuration->getRootDir();
$configuration->getApplication();
$configuration->getEnvironment();
$configuration->isDebug();
Application Config.
- app.yml
- settings.yml
- factories.yml
- view.yml
- filters.yml
- frontendConfiguration.class.php
- security.yml
- cache.yml
Module Conf.
- security.yml
- module.yml
- view.yml
- generator.yml
Environment
Keeping same configurations in parallel.
# Production environment settings
prod:
...
# Development environment settings
dev:
...
# Test environment settings
test:
...
# Custom environment settings
myenv:
...
# Settings for all environments
all:
...
Config. Caching
all
: # Setting for all environments
mail:
webmaster
: webmaster
@example
.com
=>
<?php
sfConfig
::add(array(
'app_mail_webmaster' => 'webmaster@example.com',
)); sfConfig
// Retrieve a setting
$parameter = sfConfig::get('param_name', $default_value);
// Define a setting
sfConfig::set('param_name', $value);
app.yml
all
:
.general
:
tax
: 19.6
default_user
:
name
: John Doe
mail:
webmaster
: webmaster
@example
.com
contact
: contact
@example
.com
dev
:
mail:
webmaster
: dummy
@example
.com
contact
: dummy
@example
.com
==>
echo sfConfig
::get('app_tax'); // Remember that category headers are ignored
=> '19.6'
echo sfConfig
::get('app_default_user_name');
=> 'John Doe'
echo sfConfig
::get('app_mail_webmaster');
=> 'dummy@example.com'
echo sfConfig
::get('app_mail_contact');
=> 'dummy@example.com'
all:
.settings:
csrf_secret: false
escaping_strategy: off
escaping_method: ESC_SPECIALCHARS
==>
sfConfig
::add(array(
'sf_csrf_secret' => 'false',
'sf_escaping_strategy' => 'false',
'sf_escaping_method' => 'ESC_SPECIALCHARS',
)); Foreign Keys
columns ending with _id are considered to be foreign keys
propel:
blog_article:
_attributes: { phpName: Article }
id:
title: varchar(255)
content: longvarchar
created_at:
blog_comment:
_attributes: { phpName: Comment }
id:
article_id:
author: varchar(255)
content: longvarchar
created_at:
Foregin Key
$comment = new Comment();
$comment->setAuthor('Steve');
$comment->setContent('Gee, dude, you rock: best article ever!');
// Attach this comment to the previous $article object
$comment->setArticle($article);
// Alternative syntax
// Only makes sense if the object is already saved in the database
$comment->setArticleId($article->getId());
// Many to one relationship
echo $comment->getArticle()->getTitle();
=> My first article
echo $comment->getArticle()->getContent();
=> This is my very first article
.
Hope you enjoy it
!
// One to many relationship
$comments = $article->getComments();