SlideShare a Scribd company logo
1 of 43
Download to read offline
PHPers Poznań #3
23.02.2016
Symfony
tips & tricks
http://www.slideshare.net/javier.eguiluz/new-symfony-tips-tricks-symfonycon-paris-2015
Accessing
request
parameters
/**
* @param Request $request
*
* @return JsonResponse
*/
public function getCandidateVisitAction(Request $request)
{
$candidateId = (int) $request->get('id');
$candidates = $this->getDoctrine()
->getRepository('EspeoCommonBundle:CandidateVisit')
->getVisitStatistics($candidateId);
return new JsonResponse($candidates);
}
/**
* @param Request $request
*
* @return JsonResponse
*/
public function getCandidateVisitAction(Request $request)
{
$candidateId = $request->query->getInt('id');
$candidates = $this->getDoctrine()
->getRepository('EspeoCommonBundle:CandidateVisit')
->getVisitStatistics($candidateId);
return new JsonResponse($candidates);
}
Naming
strategies
/**
* CandidateVisit.
*
* @ORMTable(name="candidate_visit")
* @ORMEntity(repositoryClass="EspeoCommonBundleEntity
RepositoryCandidateVisitRepository")
*/
class CandidateVisit
{
/**
* @var DateTime
*
* @ORMColumn(name="date_visit", type="datetime")
*/
private $dateVisit;
}
# app/config/config.yml
doctrine:
dbal:
(...)
orm:
naming_strategy: doctrine.orm.naming_strategy.underscore
<?php
namespace DoctrineORMMapping;
interface NamingStrategy
{
function classToTableName($className);
function propertyToColumnName($propertyName, $className = null);
function referenceColumnName();
function joinColumnName($propertyName);
function joinTableName($sourceEntity, $targetEntity, $propertyName);
function joinKeyColumnName($entityName, $referencedColumnName = null);
}
Twig &
app.user
{% if app.user and is_granted('ROLE_ADMIN') %}
...
{% endif %}
Email
delivery
whitelist
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool:
type: file
path: "%kernel.root_dir%/spool"
# app/config/config_dev.yml
swiftmailer:
delivery_address: dev@example.com
delivery_whitelist:
# all email addresses matching these regexes will be delivered
# like normal, as well as being sent to dev@example.com
- '/@specialdomain.com$/'
- '/^admin@mydomain.com$/'
Automatic
"smoke testing"
for services
public function testContainerServices()
{
$client = static::createClient();
foreach ($client->getContainer()->getServiceIds() as $serviceId) {
$service = $client->getContainer()->get($serviceId);
$this->assertNotNull($service);
}
}
public function testContainerServices()
{
$client = static::createClient();
foreach ($client->getContainer()->getServiceIds() as $serviceId) {
try {
$startedAt = microtime(true);
$service = $client->getContainer()->get($serviceId);
$elapsed = (microtime(true) - $startedAt) * 1000;
$this->assertLessThan(50, $elapsed);
} catch(InactiveScopeException $e) {
}
}
}
symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
ParamConverter
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SensioBundleFrameworkExtraBundleConfigurationParamConverter;
/**
* @Route("/blog/{id}")
* @ParamConverter("post", class="SensioBlogBundle:Post")
*/
public function showAction(Post $post)
{
}
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SensioBundleFrameworkExtraBundleConfigurationParamConverter;
/**
* @Route("/blog/{id}")
* @ParamConverter("post", class="SensioBlogBundle:Post", options=
{"entity_manager" = "foo"})
*/
public function showAction(Post $post)
{
}
Bundle
http://jmsyst.com/bundles/JMSDiExtraBundle
JMSDiExtraBundle
// composer.json
{
// ...
require: {
// ...
"jms/di-extra-bundle": "dev-master"
}
}
#app/AppKernel.php
$bundles = array(
// ...
new JMSDiExtraBundleJMSDiExtraBundle($this),
new JMSAopBundleJMSAopBundle(),
// ...
);
#app/config/config.yml
jms_di_extra:
locations:
all_bundles: false
bundles: [FooBundle, AcmeBlogBundle]
directories: ["%kernel.root_dir%/../src"]
<?php
namespace EspeoCandidateBundleController;
use EspeoCandidateBundleServiceCalendarEventProvider;
use EspeoCommonBundleControllerController;
use EspeoCommonBundleEntityRepositoryCitiesRepository;
use JMSDiExtraBundleAnnotation as DI;
use SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorage;
use SymfonyComponentTranslationTranslatorInterface;
class CalendarApiController extends Controller
{
/**
* @var CalendarEventProvider
* @DIInject("espeo_candidate.calendar_event_provider")
*/
private $calendarEventProvider;
/**
* @var TokenStorage
* @DIInject("security.token_storage")
*/
private $tokenStorage;
/**
* @var CitiesRepository
* @DIInject("espeo_common.cities_repository")
*/
private $citiesRepository;
}
<?php
class CalendarApiController extends Controller
{
/**
* @var CalendarEventProvider
* @DIInject("espeo_candidate.calendar_event_provider")
*/
private $calendarEventProvider;
/**
* @var TokenStorage
* @DIInject("security.token_storage")
*/
private $tokenStorage;
/**
* @var CityRepository
* @DIInject("espeo_common.city_repository")
*/
private $cityRepository;
}
private function getCoolCities()
{
$cities = $this->cityRepository->getSosnowiecAndRadom();
return $cities;
}
use JMSDiExtraBundleAnnotation as DI;
class Controller
{
private $em;
private $session;
/**
* @DIInjectParams({
* "em" = @DIInject("doctrine.orm.entity_manager"),
* "session" = @DIInject("session")
* })
*/
public function __construct($em, $session)
{
$this->em = $em;
$this->session = $session;
}
}
https://github.com/willdurand/BazingaJsTranslationBundle
BazingaJsTranslation
Bundle
composer require "willdurand/js-translation-bundle"
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new BazingaBundleJsTranslationBundleBazingaJsTranslationBundle()
);
}
#app/config/routing.yml
_bazinga_jstranslation:
resource: "@BazingaJsTranslationBundle/Resources/config/routing/routing.yml"
#app/config/assetic/js.yml
bazinga_js_translation_js:
inputs:
- '%kernel.root_dir%/../web/bundles/bazingajstranslation/js/translator.min.js'
- '%kernel.root_dir%/../web/js/translations/config.js'
- '%kernel.root_dir%/../web/js/translations/*/*.js'
compile:
php app/console bazinga:js-translation:dump
php app/console assetic:dump
{% javascripts '@bazinga_js_translation_js' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
https://github.com/boxuk/angular-symfony-translation
angular-symfony-
translation
bower install --save angular-symfony-translation
#app/config/assetic/js.yml
bazinga_js_angular_js:
inputs:
- '%kernel.root_dir%/../bower_components/angular-symfony-translation/
dist/angular-symfony-translation.js'
var app = angular.module('my_module', [
'boxuk.translation'
]);
{% javascripts '@bazinga_js_translation_js' '@bazinga_js_angular_js' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
[[ ('errand.status.in_progress') | trans ]]
#src/Espeo/CommonBundle/Resources/translations/messages.pl.yml
errand:
status:
in_progress: W trakcie
closed: Zamknięte
canceled: Odwołany
new: Nowy
ESPEO SOFTWARE
Mariusz Kozłowski
mariusz.kozlowski@espeo.eu

More Related Content

What's hot

The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010
Fabien Potencier
 
Speed up your developments with Symfony2
Speed up your developments with Symfony2Speed up your developments with Symfony2
Speed up your developments with Symfony2
Hugo Hamon
 
Keeping it small: Getting to know the Slim micro framework
Keeping it small: Getting to know the Slim micro frameworkKeeping it small: Getting to know the Slim micro framework
Keeping it small: Getting to know the Slim micro framework
Jeremy Kendall
 
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro FrameworkKeeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro Framework
Jeremy Kendall
 
News of the Symfony2 World
News of the Symfony2 WorldNews of the Symfony2 World
News of the Symfony2 World
Fabien Potencier
 

What's hot (20)

The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010
 
Symfony2 - OSIDays 2010
Symfony2 - OSIDays 2010Symfony2 - OSIDays 2010
Symfony2 - OSIDays 2010
 
How I started to love design patterns
How I started to love design patternsHow I started to love design patterns
How I started to love design patterns
 
Twig tips and tricks
Twig tips and tricksTwig tips and tricks
Twig tips and tricks
 
Speed up your developments with Symfony2
Speed up your developments with Symfony2Speed up your developments with Symfony2
Speed up your developments with Symfony2
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
 
Love and Loss: A Symfony Security Play
Love and Loss: A Symfony Security PlayLove and Loss: A Symfony Security Play
Love and Loss: A Symfony Security Play
 
High Quality Symfony Bundles tutorial - Dutch PHP Conference 2014
High Quality Symfony Bundles tutorial - Dutch PHP Conference 2014High Quality Symfony Bundles tutorial - Dutch PHP Conference 2014
High Quality Symfony Bundles tutorial - Dutch PHP Conference 2014
 
Symfony internals [english]
Symfony internals [english]Symfony internals [english]
Symfony internals [english]
 
Keeping it small: Getting to know the Slim micro framework
Keeping it small: Getting to know the Slim micro frameworkKeeping it small: Getting to know the Slim micro framework
Keeping it small: Getting to know the Slim micro framework
 
Design how your objects talk through mocking
Design how your objects talk through mockingDesign how your objects talk through mocking
Design how your objects talk through mocking
 
Sf2 wtf
Sf2 wtfSf2 wtf
Sf2 wtf
 
New in php 7
New in php 7New in php 7
New in php 7
 
Symfony2 revealed
Symfony2 revealedSymfony2 revealed
Symfony2 revealed
 
Data Validation models
Data Validation modelsData Validation models
Data Validation models
 
PhpBB meets Symfony2
PhpBB meets Symfony2PhpBB meets Symfony2
PhpBB meets Symfony2
 
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro FrameworkKeeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro Framework
 
Dependency Injection with PHP 5.3
Dependency Injection with PHP 5.3Dependency Injection with PHP 5.3
Dependency Injection with PHP 5.3
 
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
 
News of the Symfony2 World
News of the Symfony2 WorldNews of the Symfony2 World
News of the Symfony2 World
 

Viewers also liked (9)

Ain't your momma
Ain't your mommaAin't your momma
Ain't your momma
 
Submarine
SubmarineSubmarine
Submarine
 
Locations
LocationsLocations
Locations
 
Lesson progress tracking sheet
Lesson progress tracking sheetLesson progress tracking sheet
Lesson progress tracking sheet
 
[CAPITAL CLUB DINNER]- RAPOR SEMESTER 1: SETENGAH KRISIS
[CAPITAL CLUB DINNER]- RAPOR SEMESTER 1: SETENGAH KRISIS[CAPITAL CLUB DINNER]- RAPOR SEMESTER 1: SETENGAH KRISIS
[CAPITAL CLUB DINNER]- RAPOR SEMESTER 1: SETENGAH KRISIS
 
TREAT YOUR BETTER
TREAT YOUR BETTERTREAT YOUR BETTER
TREAT YOUR BETTER
 
Polscy internauci-gracze 2015
Polscy internauci-gracze 2015Polscy internauci-gracze 2015
Polscy internauci-gracze 2015
 
Mapa conceptual
Mapa conceptualMapa conceptual
Mapa conceptual
 
Evaluaci+¦n practica
Evaluaci+¦n practicaEvaluaci+¦n practica
Evaluaci+¦n practica
 

Similar to Symfony tips and tricks

Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)
Fabien Potencier
 
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf Conference
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsPhpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friends
Michael Peacock
 

Similar to Symfony tips and tricks (20)

Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hacking
 
Migrare da symfony 1 a Symfony2
 Migrare da symfony 1 a Symfony2  Migrare da symfony 1 a Symfony2
Migrare da symfony 1 a Symfony2
 
I Phone On Rails
I Phone On RailsI Phone On Rails
I Phone On Rails
 
Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using Codeception
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 
Unittests für Dummies
Unittests für DummiesUnittests für Dummies
Unittests für Dummies
 
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
 
Symfony CoP: Form component
Symfony CoP: Form componentSymfony CoP: Form component
Symfony CoP: Form component
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
 
Meet Magento Sweden - Magento 2 Layout and Code Compilation for Performance
Meet Magento Sweden - Magento 2 Layout and Code Compilation for PerformanceMeet Magento Sweden - Magento 2 Layout and Code Compilation for Performance
Meet Magento Sweden - Magento 2 Layout and Code Compilation for Performance
 
How kris-writes-symfony-apps-london
How kris-writes-symfony-apps-londonHow kris-writes-symfony-apps-london
How kris-writes-symfony-apps-london
 
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsPhpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friends
 
Virtual Madness @ Etsy
Virtual Madness @ EtsyVirtual Madness @ Etsy
Virtual Madness @ Etsy
 
How I started to love design patterns
How I started to love design patternsHow I started to love design patterns
How I started to love design patterns
 
Service approach for development REST API in Symfony2
Service approach for development REST API in Symfony2Service approach for development REST API in Symfony2
Service approach for development REST API in Symfony2
 
Magento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request FlowMagento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request Flow
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 

Symfony tips and tricks