SlideShare uma empresa Scribd logo
1 de 19
Baixar para ler offline
An Introduction to
Christian Zenker @xopn mail@xopn.de
Symfony Components
● About 20 stand-alone packages with one specific purpose
● Mostly independent from each other
● Usable in almost any PHP project
● Unit tested and well documented
http://symfony.com/components
● YAML is a human-readable data
serialization format like XML or
JSON
YAML Component
user:
name: Homer Simpson
# unsure about age
age: 40
kids: ["Bart", "Lisa", "Maggie"]
quotes:
- |
Operator! Give me the
number for 911!
- |
Internet? Is that thing
still around?"
http://symfony.com/doc/current/components/console/introduction.html
$yaml =
file_get_contents('users.yml');
$parser = new Parser();
$data = $parser->parse($yaml);
$data['user']['wife'] = 'Marge';
$dumper = new Dumper();
$yaml = $dumper->dump($data, 2);
file_put_contents(
'users.yml', $data
);
Console Component
● Write console programs in PHP
● Clean architecture
● Parses arguments and options
● Easily extensible
● Helpers for
– progress bars
– tabular data
– interactive questions
– colored output
me@home> php console.php demo:hello John
Hello John
me@home> php console.php demo:hello
What's your name? John
Hello John
me@home> php sandwich.php make
What? Make it yourself!
me@home> php sandwich.php make --force
Ok.
[=======>---------] 42%
Console Component (II)
class GreetCommand extends Command {
protected function configure() {
$this
->setName('demo:greet')
->setDescription('Greet someone')
->addArgument(
'name',
InputArgument::OPTIONAL,
'Who do you want to greet?'
)
->addOption(
'yell',
null,
InputOption::VALUE_NONE,
'If set, the task will yell in uppercase letters'
)
;
}
// ...
Console Component (III)
// ...
protected function execute(InputInterface $input, OutputInterface $output) {
$name = $input->getArgument('name');
if ($name) {
$text = 'Hello '.$name;
} else {
$text = 'Hello';
}
if ($input->getOption('yell')) {
$text = strtoupper($text);
}
$output->writeln($text);
}
}
http://symfony.com/doc/current/components/console/introduction.html
Finder Component
http://symfony.com/doc/current/components/finder.html
http://symfony.com/doc/current/components/filesystem.html
● Simple wrapper for PHP functions
● Exceptions on error
● Batch processing$finder = new Finder();
$iterator = $finder
->files()
->name('*.php')
->depth(0)
->size('>= 1K')
->in(__DIR__);
foreach ($iterator as $file) {
/** @var SplFileInfo $file */
$file->getRealpath();
}
● Search filesystem or Amazon S3
● Uses `find` or PHP functions
$filesystem = new Filesystem();
$filesystem->mkdir($dirs, 0777);
$filesystem->touch($files, time() - 86400);
$filesystem->remove($dir);
$filesystem->chown($files, $user);
$filesystem->symlink($originDir, $targetDir);
$filesystem->mirror($originDir,$targetDir);
Filesystem Component
CssSelector Component
● Converts CSS selectors into Xpath ● Query XML and HTML pages
● Use for simple functional tests
Goutte
echo CssSelector::toXPath('div.item > h4 > a');
descendant-or-self::div[contains(
concat(' ',normalize-space(@class), ' '),
' item ')]/h4/a
https://github.com/fabpot/Goutte
http://symfony.com/doc/current/components/css_selector.html
$client = new Client();
$crawler = $client->request(
'GET', 'http://example.com'
);
$link = $crawler->selectLink('Users')->link();
$crawler = $client->click($link);
$users = $crawler->filter('#users > li');
echo count($users) . ' online';
● CSS is better readable
● Xpath is more powerful
Symfony Framework
● an HTTP framework
● MVC framework? Sure, why not.
● Based on Symfony Components
and well-established third-party
libraries
● Backwards compatibility starting
with 2.3
● Console powered
● Environment aware (production,
development, testing)
● Configuration via YAML, XML,
JSON, annotations
● Fast and low memory footprint
ClassLoader
Config
Console
DependencyInjection
EventDispatcher
Finder
Form
HttpFoundation
HttpKernel
Intl
Routing
Security
Validator
...
SF Components
SwiftMailer
Doctrine
Monolog
Assetic
Twig
FrameworkBundle
SwiftmailerBundle
GeneratorBundle
DoctrineBundle
WebProfiler
Bundle
AsseticBundle Security
Bundle
...
...
GET / HTTP/1.1
Host: example.com
Accept: text/html
HTTP/1.1 200 OK
Content-Type: text/html
<!DOCTYPE html>
<p>Hello World</p>
Browser
GET / HTTP/1.1
HTTP/1.1 200 OK
Application
index.php
Symfony2 Kernel
Routing
Request URI
Controller::action
Request
Object
Response
Object
GET / HTTP/1.1
HTTP/1.1 200 OK
Application
index.php
Symfony2 Kernel
Routing
Request URI
Controller::action
Request
Object
Response
Object
Firewall
Caching
Event Handling
Controller
class MainController extends Controller {
public function helloAction($name) {
return $this->render(
'AcmeHelloBundle:Main:hello.html.twig',
array('name' => $name)
);
}
}
hello:
path: /hello/{name}
defaults:
_controller: AcmeHelloBundle:Main:hello
http://example.com/hello/world
<!DOCTYPE html>
<h1>Hello {{ name }}!</h1>
<p>Also see our
<a href="{{ path('route_name') }}">
other pages
</a>
</p>
<ul>
{% for i in 0..10 if i %2 == 0 %}
<li>{{ i }} is an even number</li>
{% endfor %}
</ul>
Service Container
class AntispamService {
/** doctrine entity manager */
protected $em;
protected $max;
public function __construct($em, $max) {
$this->em = $em;
$this->max = $max
}
public function isCommentSpam($string) {
$words = explode(' ', $string);
$dql = 'SELECT w FROM AcmeHelloBundle:Word WHERE w.word IN (?)';
$query = $this->em->createQuery($dql);
$query->setParameter(0, $words);
$blackWords = $query->execute();
return count($blackWords) > $this->max;
}
}
Service Container (II)
services:
acme_hello.antispam:
class: AcmeHelloBundleAntispamService
arguments:
- @doctrine.orm.entity_manager
- %max_blacklist_words%
public function indexAction($comment) {
$service = $this->get('acme_hello.antispam');
if($service->isCommentSpam()) {
$response = new Response();
$response->setStatusCode('403');
$response->setContent('Get off my lawn');
return $response;
}
// ... do something ...
}
Further Reading
● symfony.com
– Official homepage with
documentation, etc.
● fabien.potencier.org
– The fancy parts
● silex.sensiolabs.org
– Microframework (the missing link
between Components and
Framework)
● knpbundles.com
– Database with additional bundles
– StofDoctrineExtensionsBundle
– LiipImagineBundle
– FOQElasticaBundle
– DoctrineFixturesBundle
?Questions?
!Thank you for your
attention
Christian Zenker
AOE GmbH
http://xopn.de
mail@xopn.de
Twitter @xopn

Mais conteúdo relacionado

Mais procurados

PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...webhostingguy
 
Web Development Course: PHP lecture 4
Web Development Course: PHP  lecture 4Web Development Course: PHP  lecture 4
Web Development Course: PHP lecture 4Gheyath M. Othman
 
Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3Gheyath M. Othman
 
Database schema management in Ruby apps
Database schema management in Ruby appsDatabase schema management in Ruby apps
Database schema management in Ruby appsVsevolod Romashov
 
Doing more with LESS
Doing more with LESSDoing more with LESS
Doing more with LESSjsmith92
 
Inside Bokete: Web Application with Mojolicious and others
Inside Bokete:  Web Application with Mojolicious and othersInside Bokete:  Web Application with Mojolicious and others
Inside Bokete: Web Application with Mojolicious and othersYusuke Wada
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHPAhmed Swilam
 
Intro to php
Intro to phpIntro to php
Intro to phpSp Singh
 
Php Tutorial | Introduction Demo | Basics
 Php Tutorial | Introduction Demo | Basics Php Tutorial | Introduction Demo | Basics
Php Tutorial | Introduction Demo | BasicsShubham Kumar Singh
 

Mais procurados (20)

Modern Perl
Modern PerlModern Perl
Modern Perl
 
Php workshop L03 superglobals
Php workshop L03 superglobalsPhp workshop L03 superglobals
Php workshop L03 superglobals
 
PHP7 Presentation
PHP7 PresentationPHP7 Presentation
PHP7 Presentation
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
 
Php workshop L04 database
Php workshop L04 databasePhp workshop L04 database
Php workshop L04 database
 
Using PHP
Using PHPUsing PHP
Using PHP
 
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
 
Web Development Course: PHP lecture 4
Web Development Course: PHP  lecture 4Web Development Course: PHP  lecture 4
Web Development Course: PHP lecture 4
 
Lumberjack XPath 101
Lumberjack XPath 101Lumberjack XPath 101
Lumberjack XPath 101
 
Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3
 
Sa
SaSa
Sa
 
Database schema management in Ruby apps
Database schema management in Ruby appsDatabase schema management in Ruby apps
Database schema management in Ruby apps
 
New in php 7
New in php 7New in php 7
New in php 7
 
Doing more with LESS
Doing more with LESSDoing more with LESS
Doing more with LESS
 
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDESPHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
 
RESTful web services
RESTful web servicesRESTful web services
RESTful web services
 
Inside Bokete: Web Application with Mojolicious and others
Inside Bokete:  Web Application with Mojolicious and othersInside Bokete:  Web Application with Mojolicious and others
Inside Bokete: Web Application with Mojolicious and others
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHP
 
Intro to php
Intro to phpIntro to php
Intro to php
 
Php Tutorial | Introduction Demo | Basics
 Php Tutorial | Introduction Demo | Basics Php Tutorial | Introduction Demo | Basics
Php Tutorial | Introduction Demo | Basics
 

Semelhante a An Introduction to Symfony

Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Hugo Hamon
 
Simplify your professional web development with symfony
Simplify your professional web development with symfonySimplify your professional web development with symfony
Simplify your professional web development with symfonyFrancois Zaninotto
 
PHP SA 2014 - Releasing Your Open Source Project
PHP SA 2014 - Releasing Your Open Source ProjectPHP SA 2014 - Releasing Your Open Source Project
PHP SA 2014 - Releasing Your Open Source Projectxsist10
 
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years laterSymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years laterHaehnchen
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy CodeRowan Merewood
 
Hands-on with the Symfony2 Framework
Hands-on with the Symfony2 FrameworkHands-on with the Symfony2 Framework
Hands-on with the Symfony2 FrameworkRyan Weaver
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Muhamad Al Imran
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Muhamad Al Imran
 
From CakePHP to Laravel
From CakePHP to LaravelFrom CakePHP to Laravel
From CakePHP to LaravelJason McCreary
 
Creating a modern web application using Symfony API Platform Atlanta
Creating a modern web application using  Symfony API Platform AtlantaCreating a modern web application using  Symfony API Platform Atlanta
Creating a modern web application using Symfony API Platform AtlantaJesus Manuel Olivas
 
Python RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutionsPython RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutionsSolution4Future
 
7 Tips on Getting Your Theme Approved the First Time
7 Tips on Getting Your Theme Approved the First Time7 Tips on Getting Your Theme Approved the First Time
7 Tips on Getting Your Theme Approved the First TimeDmitry Mayorov
 
Symfony console: build awesome command line scripts with ease
Symfony console: build awesome command line scripts with easeSymfony console: build awesome command line scripts with ease
Symfony console: build awesome command line scripts with easeOscar Merida
 
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry PiGrâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry PiJérémy Derussé
 
Hacking IIS - NahamCon.pdf
Hacking IIS - NahamCon.pdfHacking IIS - NahamCon.pdf
Hacking IIS - NahamCon.pdfdistortdistort
 

Semelhante a An Introduction to Symfony (20)

Symfony2 revealed
Symfony2 revealedSymfony2 revealed
Symfony2 revealed
 
Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2
 
Simplify your professional web development with symfony
Simplify your professional web development with symfonySimplify your professional web development with symfony
Simplify your professional web development with symfony
 
PHP SA 2014 - Releasing Your Open Source Project
PHP SA 2014 - Releasing Your Open Source ProjectPHP SA 2014 - Releasing Your Open Source Project
PHP SA 2014 - Releasing Your Open Source Project
 
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years laterSymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Hands-on with the Symfony2 Framework
Hands-on with the Symfony2 FrameworkHands-on with the Symfony2 Framework
Hands-on with the Symfony2 Framework
 
PhpBB meets Symfony2
PhpBB meets Symfony2PhpBB meets Symfony2
PhpBB meets Symfony2
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
From CakePHP to Laravel
From CakePHP to LaravelFrom CakePHP to Laravel
From CakePHP to Laravel
 
Symfony 4 & Flex news
Symfony 4 & Flex newsSymfony 4 & Flex news
Symfony 4 & Flex news
 
Creating a modern web application using Symfony API Platform Atlanta
Creating a modern web application using  Symfony API Platform AtlantaCreating a modern web application using  Symfony API Platform Atlanta
Creating a modern web application using Symfony API Platform Atlanta
 
Python RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutionsPython RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutions
 
7 Tips on Getting Your Theme Approved the First Time
7 Tips on Getting Your Theme Approved the First Time7 Tips on Getting Your Theme Approved the First Time
7 Tips on Getting Your Theme Approved the First Time
 
Symfony console: build awesome command line scripts with ease
Symfony console: build awesome command line scripts with easeSymfony console: build awesome command line scripts with ease
Symfony console: build awesome command line scripts with ease
 
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry PiGrâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
 
Hacking IIS - NahamCon.pdf
Hacking IIS - NahamCon.pdfHacking IIS - NahamCon.pdf
Hacking IIS - NahamCon.pdf
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 

Último

2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 

Último (20)

2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 

An Introduction to Symfony

  • 1. An Introduction to Christian Zenker @xopn mail@xopn.de
  • 2. Symfony Components ● About 20 stand-alone packages with one specific purpose ● Mostly independent from each other ● Usable in almost any PHP project ● Unit tested and well documented http://symfony.com/components
  • 3. ● YAML is a human-readable data serialization format like XML or JSON YAML Component user: name: Homer Simpson # unsure about age age: 40 kids: ["Bart", "Lisa", "Maggie"] quotes: - | Operator! Give me the number for 911! - | Internet? Is that thing still around?" http://symfony.com/doc/current/components/console/introduction.html $yaml = file_get_contents('users.yml'); $parser = new Parser(); $data = $parser->parse($yaml); $data['user']['wife'] = 'Marge'; $dumper = new Dumper(); $yaml = $dumper->dump($data, 2); file_put_contents( 'users.yml', $data );
  • 4. Console Component ● Write console programs in PHP ● Clean architecture ● Parses arguments and options ● Easily extensible ● Helpers for – progress bars – tabular data – interactive questions – colored output me@home> php console.php demo:hello John Hello John me@home> php console.php demo:hello What's your name? John Hello John me@home> php sandwich.php make What? Make it yourself! me@home> php sandwich.php make --force Ok. [=======>---------] 42%
  • 5. Console Component (II) class GreetCommand extends Command { protected function configure() { $this ->setName('demo:greet') ->setDescription('Greet someone') ->addArgument( 'name', InputArgument::OPTIONAL, 'Who do you want to greet?' ) ->addOption( 'yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters' ) ; } // ...
  • 6. Console Component (III) // ... protected function execute(InputInterface $input, OutputInterface $output) { $name = $input->getArgument('name'); if ($name) { $text = 'Hello '.$name; } else { $text = 'Hello'; } if ($input->getOption('yell')) { $text = strtoupper($text); } $output->writeln($text); } } http://symfony.com/doc/current/components/console/introduction.html
  • 7. Finder Component http://symfony.com/doc/current/components/finder.html http://symfony.com/doc/current/components/filesystem.html ● Simple wrapper for PHP functions ● Exceptions on error ● Batch processing$finder = new Finder(); $iterator = $finder ->files() ->name('*.php') ->depth(0) ->size('>= 1K') ->in(__DIR__); foreach ($iterator as $file) { /** @var SplFileInfo $file */ $file->getRealpath(); } ● Search filesystem or Amazon S3 ● Uses `find` or PHP functions $filesystem = new Filesystem(); $filesystem->mkdir($dirs, 0777); $filesystem->touch($files, time() - 86400); $filesystem->remove($dir); $filesystem->chown($files, $user); $filesystem->symlink($originDir, $targetDir); $filesystem->mirror($originDir,$targetDir); Filesystem Component
  • 8. CssSelector Component ● Converts CSS selectors into Xpath ● Query XML and HTML pages ● Use for simple functional tests Goutte echo CssSelector::toXPath('div.item > h4 > a'); descendant-or-self::div[contains( concat(' ',normalize-space(@class), ' '), ' item ')]/h4/a https://github.com/fabpot/Goutte http://symfony.com/doc/current/components/css_selector.html $client = new Client(); $crawler = $client->request( 'GET', 'http://example.com' ); $link = $crawler->selectLink('Users')->link(); $crawler = $client->click($link); $users = $crawler->filter('#users > li'); echo count($users) . ' online'; ● CSS is better readable ● Xpath is more powerful
  • 9. Symfony Framework ● an HTTP framework ● MVC framework? Sure, why not. ● Based on Symfony Components and well-established third-party libraries ● Backwards compatibility starting with 2.3 ● Console powered ● Environment aware (production, development, testing) ● Configuration via YAML, XML, JSON, annotations ● Fast and low memory footprint
  • 11. GET / HTTP/1.1 Host: example.com Accept: text/html HTTP/1.1 200 OK Content-Type: text/html <!DOCTYPE html> <p>Hello World</p> Browser
  • 12. GET / HTTP/1.1 HTTP/1.1 200 OK Application index.php Symfony2 Kernel Routing Request URI Controller::action Request Object Response Object
  • 13. GET / HTTP/1.1 HTTP/1.1 200 OK Application index.php Symfony2 Kernel Routing Request URI Controller::action Request Object Response Object Firewall Caching Event Handling
  • 14. Controller class MainController extends Controller { public function helloAction($name) { return $this->render( 'AcmeHelloBundle:Main:hello.html.twig', array('name' => $name) ); } } hello: path: /hello/{name} defaults: _controller: AcmeHelloBundle:Main:hello http://example.com/hello/world <!DOCTYPE html> <h1>Hello {{ name }}!</h1> <p>Also see our <a href="{{ path('route_name') }}"> other pages </a> </p> <ul> {% for i in 0..10 if i %2 == 0 %} <li>{{ i }} is an even number</li> {% endfor %} </ul>
  • 15. Service Container class AntispamService { /** doctrine entity manager */ protected $em; protected $max; public function __construct($em, $max) { $this->em = $em; $this->max = $max } public function isCommentSpam($string) { $words = explode(' ', $string); $dql = 'SELECT w FROM AcmeHelloBundle:Word WHERE w.word IN (?)'; $query = $this->em->createQuery($dql); $query->setParameter(0, $words); $blackWords = $query->execute(); return count($blackWords) > $this->max; } }
  • 16. Service Container (II) services: acme_hello.antispam: class: AcmeHelloBundleAntispamService arguments: - @doctrine.orm.entity_manager - %max_blacklist_words% public function indexAction($comment) { $service = $this->get('acme_hello.antispam'); if($service->isCommentSpam()) { $response = new Response(); $response->setStatusCode('403'); $response->setContent('Get off my lawn'); return $response; } // ... do something ... }
  • 17. Further Reading ● symfony.com – Official homepage with documentation, etc. ● fabien.potencier.org – The fancy parts ● silex.sensiolabs.org – Microframework (the missing link between Components and Framework) ● knpbundles.com – Database with additional bundles – StofDoctrineExtensionsBundle – LiipImagineBundle – FOQElasticaBundle – DoctrineFixturesBundle
  • 19. !Thank you for your attention Christian Zenker AOE GmbH http://xopn.de mail@xopn.de Twitter @xopn