SlideShare a Scribd company logo
The Naked Bundle 
Matthias Noback 
@matthiasnoback 
Symfony Usergroup Belgium 
October 13th, 2014
What's it all about?
An actual naked bundle
I could've called it 
BundleLitetm 
The No Code Bundle 
The Clean Bundle
But “naked” was catchy and controversial
Until Pierre came along
The official view on bundles
First-class citizens 
Documentation » The Quick Tour » The Architecture
Importance 
Your code is more important than 
the framework, 
which is an implementation detail
Reuse
Nice!
All your code lives in a bundle 
Documentation » The Book » Creating Pages in Symfony2
Reuse 
“All your code in a bundle” 
contradicts the promise of reuse
Everything lives inside a bundle 
Documentation » Glossary
Not really true 
Many things live inside libraries 
(the Symfony components are 
libraries too!)
Which is good!
But you probably know that already
“libraries first”
What about... 
● Controllers 
● Entities 
● Templates 
● ...
They just need to be in a bundle 
Or do they?
Don't get me wrong 
I love Symfony!
But a framework is just a framework 
● Quickstarter for your projects 
● Prevents and solves big security 
issues for you 
● Has a community you can rely on
A framework is there for you
Your code doesn't need a framework
Noback's Principle 
Code shouldn't rely on something 
it doesn't truly need
Bundle conventions 
Things in a bundle often rely on 
conventions to work
Conventions aren't necessary at all
So according to Noback's Principle, 
code shouldn't rely on bundle conventions too
Naming conventions 
Controllers: 
● *Controller classes 
● *action methods 
Templates: 
● in /Resources/views 
● name: Controller/Action.html.twig
Structural conventions 
Controller: 
● Extends framework Controller class 
● Is ContainerAware
Behavioral conventions 
Controller: 
● Is allowed to return an array 
● Actions can type-hint to objects which will be 
fetched based on route parameters (??)
Configuration conventions 
Use lots of annotations! 
/** 
* @Route("/{id}") 
* @Method("GET") 
* @ParamConverter("post", class="SensioBlogBundle:Post") 
* @Template("SensioBlogBundle:Annot:show.html.twig") 
* @Cache(smaxage="15", lastmodified="post.getUpdatedAt()") 
* @Security("has_role('ROLE_ADMIN')") 
*/ 
public function showAction(Post $post) 
{ 
}
These conventions are what makes an application 
a Symfony2 application
A Year With Symfony
About bundles
A bundle exposes resources
Resources 
● Service definitions 
● Controllers 
● Routes 
● Templates 
● Entities 
● Form types 
● Event listeners 
● Translations 
● ...
No need for them to be inside a bundle
When placed outside the bundle 
the resources could be reused separately
The bundle would be really small
And could just as well be a: 
Laravel package, 
Zend or Drupal module, 
CakePHP plugin, 
...
So the challenge is to 
Make the bundle as clean as possible
Move the “misplaced” things to 
● a library 
● with dependencies 
● but not symfony/framework­bundle 
;)
Being realistic 
Practical reusability
Reuse within the Symfony family 
Think: Silex, Laravel, etc.
Allowed dependency 
HttpFoundation 
● Request 
● Response 
● Exceptions 
● etc.
What do we rely on 
HttpKernel 
namespace SymfonyComponentHttpKernel; 
use SymfonyComponentHttpFoundationRequest; 
use SymfonyComponentHttpFoundationResponse; 
interface HttpKernelInterface 
{ 
/** 
* Handles a Request to convert it to a Response. 
*/ 
public function handle(Request $request, ...); 
}
Why? My secret missions 
“Let's rebuild the application, but 
this time we use Zend4 instead of 
Symfony2”
And of course 
Education
You need a strong coupling radar
Explicit dependencies 
● Function calls 
● Imported classes (“use”) 
● Included files 
● ...
Implicit dependencies 
● File locations 
● File, class, method names 
● Structure of return values 
● ...
There we go!
use SymfonyBundleFrameworkBundleControllerController; 
use SensioBundleFrameworkExtraBundleConfigurationRoute; 
use SensioBundleFrameworkExtraBundleConfigurationTemplate; 
/** 
* @Route(“/article”) 
*/ 
class ArticleController extends Controller 
{ 
/** 
* @Route(“/edit”) 
* @Template() 
*/ 
function editAction(...) 
{ 
... 
} 
} 
Controller
TODO 
✔ Don't rely on things that may not 
be there in another context: 
✔ Parent Controller class 
✔ Routing, template, annotations, etc.
class ArticleController 
{ 
function editAction(...) 
{ 
... 
} 
} 
Nice and clean ;)
use SymfonyComponentHttpFoundationRequest; 
class ArticleController 
{ 
public function editAction(Request $request) 
{ 
$em = $this­> 
get('doctrine')­> 
getManager(); 
... 
if (...) { 
throw $this­> 
createNotFoundException(); 
} 
... 
return array( 
'form' => $form­> 
createView() 
); 
} 
} 
Zooming in a bit
TODO 
✔ Inject dependencies 
✔ Don't use helper methods 
✔ Render the template manually 
✔ Keep using Request 
(not really a TODO)
use DoctrineORMEntityManager; 
class ArticleController 
{ 
function __construct( 
EntityManager $em, 
) { 
$this­> 
em = $em; 
} 
... 
} 
Inject dependencies
Inline helper methods 
use SymfonyComponentHttpKernelExceptionNotFoundHttpException 
class ArticleController 
{ 
... 
public function newAction(...) 
{ 
... 
throw new NotFoundHttpException(); 
... 
} 
}
use SymfonyComponentHttpFoundationResponse; 
use SymfonyComponentTemplatingEngineInterface; 
class ArticleController 
{ 
function __construct(..., EngineInterface $templating) { 
} 
public function newAction(...) 
{ 
... 
return new Response( 
$this­> 
templating­> 
render( 
'@MyBundle:Article:new.html.twig', 
array( 
'form' => $form­> 
createView() 
) 
) 
); 
} 
} 
Render the template manually
Dependencies are explicit now 
Also: no mention of a “bundle” 
anywhere! 
use SymfonyComponentHttpFoundationRequest; 
use SymfonyComponentHttpFoundationResponse; 
use SymfonyComponentTemplatingEngineInterface; 
use DoctrineORMEntityManager;
Dependency overflow 
use SymfonyComponentHttpFoundationResponse; 
use SymfonyComponentTemplatingEngineInterface; 
class ArticleController 
{ 
function __construct( 
EntityManager $entityManager, 
EngineInterface $templating, 
TranslatorInterface $translator, 
ValidatorInterface $validator, 
Swift_Mailer $mailer, 
RouterInterface $router 
) { 
... 
} 
... 
}
The cause? 
Convention
One controller, many actions 
one action!
__invoke() 
namespace MyLibraryControllerArticle; 
class New 
{ 
function __construct(...) 
{ 
// only what's necessary for this “action” 
} 
public function __invoke(...) 
{ 
... 
} 
}
Nice! 
└─Controller 
└─Article 
├─New.php 
├─Edit.php 
├─Archive.php 
└─Delete.php
TODO 
✔ Set up routing 
✔ Create a service and provide the 
right arguments
Bundle stuff: services.xml 
<!­­in 
MyBundle/Resources/config/services.xml → 
<?xml version="1.0" ?> 
<container> 
<services> 
<service id="new_article_controller" 
class="MyBundleControllerArticleNew"> 
<argument type="service" 
id="doctrine.orm.default_entity_manager" /> 
<argument type="service" id="templating" /> 
</service> 
</services> 
</container>
Bundle stuff: routing.xml 
<!­­in 
MyBundle/Resources/config/routing.xml → 
<?xml version="1.0" encoding="UTF­8" 
?> 
<routes> 
<route id="new_article" 
path="/article/new"> 
<default key="_controller"> 
new_article_controller:__invoke 
</default> 
</route> 
</routes> 
Pull request by Kevin Bond allows you to leave out the “:__invoke” part!
Controller – Achievements 
● Can be anywhere 
● No need to follow naming conventions 
(“*Controller”, “*action”) 
● Dependency injection, no service location 
● Reusable in any application using 
HttpFoundation
Next up: Entities
namespace MyBundleEntity; 
use DoctrineORMMapping as ORM; 
class Article 
{ 
... 
/** 
* @ORMColumn(type=”string”) 
*/ 
private $title; 
} 
Entity conventions
What's wrong with annotations? 
namespace MyBundleEntity; 
use DoctrineORMMapping as ORM; 
class Article 
{ 
... 
/** 
* @ORMColumn(type=”string”) 
*/ 
private $title; 
}
Annotations are classes
use DoctrineCommonAnnotationsAnnotationReader; 
$reader = new AnnotationReader(); 
$class = new ReflectionClass('Article'); 
$reader­> 
getClassAnnotations($class); 
BANG 
Class 
DoctrineORMMappingColumn 
not found
Well, uhm, yes, but...
Are you ever going to use 
anything else than Doctrine ORM?
Well... 
Think about Doctrine MongoDB ODM, 
Doctrine CouchDB ODM, etc. 
namespace MyBundleEntity; 
use DoctrineORMMapping as ORM; 
use DoctrineODMMongoDBMappingAnnotations as MongoDB; 
use DoctrineODMCouchDBMappingAnnotations as CoucheDB; 
class Article 
{ 
/** 
* @ORMColumn 
* @MognoDBField 
* @CoucheDBField 
*/ 
private $title; 
}
TODO 
✔ Remove annotations 
✔ Find another way to map the data
namespace MyBundleEntity; 
class Article 
{ 
private $id; 
private $title; 
} 
Nice and clean 
A true POPO, the ideal of the data 
mapper pattern
Use XML for mapping metadata 
<doctrine­mapping> 
<entity name=”MyBundleEntityArticle”> 
<id name="id" type="integer" column="id"> 
<generator strategy="AUTO"/> 
</id> 
<field name=”title” type=”string”> 
</entity> 
</doctrine­mapping>
Conventions for XML metadata 
● For MyBundleEntityArticle 
● Put XML here: 
@MyBundle/Resources/config/doctrine/ 
Article.orm.xml
We don't want it in the bundle! 
There's a nice little trick
You need DoctrineBundle >=1.2 
{ 
"require": { 
..., 
"doctrine/doctrine­bundle": 
"~1.2@dev" 
} 
}
use DoctrineBundleDoctrineBundleDependencyInjectionCompiler 
DoctrineOrmMappingsPass; 
class MyBundle extends Bundle 
{ 
public function build(ContainerBuilder $container) 
{ 
$container­> 
addCompilerPass( 
$this­> 
buildMappingCompilerPass() 
); 
} 
private function buildMappingCompilerPass() 
{ 
$xmlPath = '%kernel.root_dir%/../src/MyLibrary/Doctrine'; 
$namespacePrefix = 'MyLibraryModel'; 
return DoctrineOrmMappingsPass::createXmlMappingDriver( 
array($xmlPath => $namespacePrefix) 
); 
} 
}
Now: 
● For MyLibraryModelArticle 
● Put XML here: 
src/MyLibrary/Doctrine/Article.orm.xml
Entities - Achievements 
● Entity classes can be anywhere 
● Mapping metadata can be 
anywhere and in different formats 
● Entities are true POPOs
Finally: Templates
Conventions 
● In /Resources/views/[Controller] 
● Filename: [Action].[format].[engine]
The difficulty with templates 
They can have all kinds of implicit 
dependencies: 
● global variables, e.g. {{ app.request }} 
● functions, e.g. {{ path(...) }} 
● parent templates, e.g. {% extends 
“::base.html.twig” %}
Still, we want them out! 
And it's possible
Documentation » The Cookbook » Templating » 
How to use and Register namespaced Twig Paths 
# in config.yml 
twig: 
... 
paths: 
Twig namespaces 
"%kernel.root_dir%/../src/MyLibrary/Views": MyLibrary 
// in the controller 
return $this­> 
templating­> 
render('@MyLibrary/Template.html.twig');
Get rid of absolute paths 
Using Puli, created by Bernhard 
Schüssek (Symfony Forms, 
Validation)
What Puli does 
Find the absolute paths of 
resources in a project
use WebmozartPuliRepositoryResourceRepository; 
$repo = new ResourceRepository(); 
$repo­> 
add('/my­library/ 
views', '/absolute/path/to/views/*'); 
/my-library/views /index.html.twig 
/absolute/path/to/views /index.html.twig 
echo $repo 
­> 
get('/my­library/ 
views/index.html.twig') 
­> 
getRealPath(); 
// => /absolute/path/to/views/index.html.twig
Register “prefixes” 
Manually, or using the 
Puli Composer plugin 
// in the composer.json file of a package or project 
{ 
"extra": { 
"resources": { 
"/my­library/ 
views": "src/MyLibrary/Views" 
} 
} 
}
Twig templates 
// in composer.json 
{ 
"extra": { 
"resources": { 
"/my­library/ 
views": "src/MyLibrary/Views" 
} 
} 
} 
Puli Twig extension 
// in the controller 
return $this­> 
templating 
­> 
render('/my­library/ 
views/index.html.twig');
Many possibilities 
● Templates 
● Translation files 
●Mapping metadata 
● Service definitions 
● And so on!
The future is bright 
● Puli is not stable yet 
● But I expect much from it:
Puli will be the ultimate tool
to create NAKED BUNDLES
and to enable reuse of 
many kinds of resources
not limited by 
project, 
framework, 
even language 
boundaries!
But even without Puli 
There's a whole lot you can do to make your code 
not rely on the framework
Remember 
The framework is for you 
Your code doesn't need it
Questions?
Buy it for $ 17,50 
http://leanpub.com/a-year-with-symfony/c/symfony-usergroup-belgium
Get a $10,00 introduction discount: 
http://leanpub.com/principles-of-php-package-design 
/c/symfony-usergroup-belgium
Thank you 
Feedback: meetup.com 
Talk to me: @matthiasnoback
Images 
Sally MacKenzie: 
www.amazon.com

More Related Content

What's hot

iOS 2 - The practical Stuff
iOS 2 - The practical StuffiOS 2 - The practical Stuff
iOS 2 - The practical StuffPetr Dvorak
 
Javascript tid-bits
Javascript tid-bitsJavascript tid-bits
Javascript tid-bits
David Atchley
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
Manvendra Singh
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
Mats Bryntse
 
Javascript and Jquery Best practices
Javascript and Jquery Best practicesJavascript and Jquery Best practices
Javascript and Jquery Best practices
Sultan Khan
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
Parte II Objective C
Parte II   Objective CParte II   Objective C
Parte II Objective C
Paolo Quadrani
 
JS Level Up: Prototypes
JS Level Up: PrototypesJS Level Up: Prototypes
JS Level Up: Prototypes
Vernon Kesner
 
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?
Kyle Oba
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best Practices
Doris Chen
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
Hoat Le
 
Objective c runtime
Objective c runtimeObjective c runtime
Objective c runtime
Inferis
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
Fu Cheng
 
Javascript Common Design Patterns
Javascript Common Design PatternsJavascript Common Design Patterns
Javascript Common Design Patterns
Pham Huy Tung
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic courseTran Khoa
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]Kuba Břečka
 

What's hot (20)

iOS 2 - The practical Stuff
iOS 2 - The practical StuffiOS 2 - The practical Stuff
iOS 2 - The practical Stuff
 
Javascript tid-bits
Javascript tid-bitsJavascript tid-bits
Javascript tid-bits
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
Javascript and Jquery Best practices
Javascript and Jquery Best practicesJavascript and Jquery Best practices
Javascript and Jquery Best practices
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
 
Parte II Objective C
Parte II   Objective CParte II   Objective C
Parte II Objective C
 
JS Level Up: Prototypes
JS Level Up: PrototypesJS Level Up: Prototypes
JS Level Up: Prototypes
 
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best Practices
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
 
Runtime
RuntimeRuntime
Runtime
 
Objective c runtime
Objective c runtimeObjective c runtime
Objective c runtime
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Javascript Common Design Patterns
Javascript Common Design PatternsJavascript Common Design Patterns
Javascript Common Design Patterns
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic course
 
Dart Workshop
Dart WorkshopDart Workshop
Dart Workshop
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]
 

Similar to The Naked Bundle - Symfony Usergroup Belgium

The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014
Matthias Noback
 
The Naked Bundle - Tryout
The Naked Bundle - TryoutThe Naked Bundle - Tryout
The Naked Bundle - Tryout
Matthias Noback
 
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)New Symfony Tips & Tricks (SymfonyCon Paris 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
Javier Eguiluz
 
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
Matthias Noback
 
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Antonio Peric-Mazar
 
Symfony tips and tricks
Symfony tips and tricksSymfony tips and tricks
Symfony tips and tricks
Javier Eguiluz
 
Symfony finally swiped right on envvars
Symfony finally swiped right on envvarsSymfony finally swiped right on envvars
Symfony finally swiped right on envvars
Sam Marley-Jarrett
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
David McCarter
 
Hands-on with the Symfony2 Framework
Hands-on with the Symfony2 FrameworkHands-on with the Symfony2 Framework
Hands-on with the Symfony2 Framework
Ryan Weaver
 
The Beauty And The Beast Php N W09
The Beauty And The Beast Php N W09The Beauty And The Beast Php N W09
The Beauty And The Beast Php N W09
Bastian Feder
 
Dependency injection in Drupal 8
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8Alexei Gorobets
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
ciklum_ods
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
Rowan Merewood
 
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
Hugo Hamon
 
Symfony2 revealed
Symfony2 revealedSymfony2 revealed
Symfony2 revealed
Fabien Potencier
 
C# Unit 2 notes
C# Unit 2 notesC# Unit 2 notes
C# Unit 2 notes
Sudarshan Dhondaley
 
Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Reusable Ruby • Rt 9 Ruby Group • Jun 2012Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Reusable Ruby • Rt 9 Ruby Group • Jun 2012skinandbones
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Python
dn
 
Java Basics
Java BasicsJava Basics
Java Basics
shivamgarg_nitj
 
Global objects in Node.pdf
Global objects in Node.pdfGlobal objects in Node.pdf
Global objects in Node.pdf
SudhanshiBakre1
 

Similar to The Naked Bundle - Symfony Usergroup Belgium (20)

The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014
 
The Naked Bundle - Tryout
The Naked Bundle - TryoutThe Naked Bundle - Tryout
The Naked Bundle - Tryout
 
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)New Symfony Tips & Tricks (SymfonyCon Paris 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
 
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
 
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
 
Symfony tips and tricks
Symfony tips and tricksSymfony tips and tricks
Symfony tips and tricks
 
Symfony finally swiped right on envvars
Symfony finally swiped right on envvarsSymfony finally swiped right on envvars
Symfony finally swiped right on envvars
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
 
Hands-on with the Symfony2 Framework
Hands-on with the Symfony2 FrameworkHands-on with the Symfony2 Framework
Hands-on with the Symfony2 Framework
 
The Beauty And The Beast Php N W09
The Beauty And The Beast Php N W09The Beauty And The Beast Php N W09
The Beauty And The Beast Php N W09
 
Dependency injection in Drupal 8
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
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
 
Symfony2 revealed
Symfony2 revealedSymfony2 revealed
Symfony2 revealed
 
C# Unit 2 notes
C# Unit 2 notesC# Unit 2 notes
C# Unit 2 notes
 
Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Reusable Ruby • Rt 9 Ruby Group • Jun 2012Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Reusable Ruby • Rt 9 Ruby Group • Jun 2012
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Python
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Global objects in Node.pdf
Global objects in Node.pdfGlobal objects in Node.pdf
Global objects in Node.pdf
 

More from Matthias Noback

Rector fireside chat - PHPMiNDS meetup
Rector fireside chat - PHPMiNDS meetupRector fireside chat - PHPMiNDS meetup
Rector fireside chat - PHPMiNDS meetup
Matthias Noback
 
Service abstractions - Part 1: Queries
Service abstractions - Part 1: QueriesService abstractions - Part 1: Queries
Service abstractions - Part 1: Queries
Matthias Noback
 
Hexagonal Symfony - SymfonyCon Amsterdam 2019
Hexagonal Symfony - SymfonyCon Amsterdam 2019Hexagonal Symfony - SymfonyCon Amsterdam 2019
Hexagonal Symfony - SymfonyCon Amsterdam 2019
Matthias Noback
 
Advanced web application architecture - PHP Barcelona
Advanced web application architecture  - PHP BarcelonaAdvanced web application architecture  - PHP Barcelona
Advanced web application architecture - PHP Barcelona
Matthias Noback
 
A testing strategy for hexagonal applications
A testing strategy for hexagonal applicationsA testing strategy for hexagonal applications
A testing strategy for hexagonal applications
Matthias Noback
 
Advanced web application architecture - Talk
Advanced web application architecture - TalkAdvanced web application architecture - Talk
Advanced web application architecture - Talk
Matthias Noback
 
DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...
DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...
DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...
Matthias Noback
 
Layers, ports and adapters
Layers, ports and adaptersLayers, ports and adapters
Layers, ports and adapters
Matthias Noback
 
Beyond design principles and patterns (muCon 2019 edition)
Beyond design principles and patterns (muCon 2019 edition)Beyond design principles and patterns (muCon 2019 edition)
Beyond design principles and patterns (muCon 2019 edition)
Matthias Noback
 
Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Brutal refactoring, lying code, the Churn, and other emotional stories from L...Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Matthias Noback
 
Advanced web application architecture Way2Web
Advanced web application architecture Way2WebAdvanced web application architecture Way2Web
Advanced web application architecture Way2Web
Matthias Noback
 
Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Brutal refactoring, lying code, the Churn, and other emotional stories from L...Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Matthias Noback
 
Beyond Design Principles and Patterns
Beyond Design Principles and PatternsBeyond Design Principles and Patterns
Beyond Design Principles and Patterns
Matthias Noback
 
Building Autonomous Services
Building Autonomous ServicesBuilding Autonomous Services
Building Autonomous Services
Matthias Noback
 
Advanced Application Architecture Symfony Live Berlin 2018
Advanced Application Architecture Symfony Live Berlin 2018Advanced Application Architecture Symfony Live Berlin 2018
Advanced Application Architecture Symfony Live Berlin 2018
Matthias Noback
 
Designing for Autonomy
Designing for AutonomyDesigning for Autonomy
Designing for Autonomy
Matthias Noback
 
Docker workshop
Docker workshopDocker workshop
Docker workshop
Matthias Noback
 
Docker swarm workshop
Docker swarm workshopDocker swarm workshop
Docker swarm workshop
Matthias Noback
 
Docker compose workshop
Docker compose workshopDocker compose workshop
Docker compose workshop
Matthias Noback
 
Building autonomous services
Building autonomous servicesBuilding autonomous services
Building autonomous services
Matthias Noback
 

More from Matthias Noback (20)

Rector fireside chat - PHPMiNDS meetup
Rector fireside chat - PHPMiNDS meetupRector fireside chat - PHPMiNDS meetup
Rector fireside chat - PHPMiNDS meetup
 
Service abstractions - Part 1: Queries
Service abstractions - Part 1: QueriesService abstractions - Part 1: Queries
Service abstractions - Part 1: Queries
 
Hexagonal Symfony - SymfonyCon Amsterdam 2019
Hexagonal Symfony - SymfonyCon Amsterdam 2019Hexagonal Symfony - SymfonyCon Amsterdam 2019
Hexagonal Symfony - SymfonyCon Amsterdam 2019
 
Advanced web application architecture - PHP Barcelona
Advanced web application architecture  - PHP BarcelonaAdvanced web application architecture  - PHP Barcelona
Advanced web application architecture - PHP Barcelona
 
A testing strategy for hexagonal applications
A testing strategy for hexagonal applicationsA testing strategy for hexagonal applications
A testing strategy for hexagonal applications
 
Advanced web application architecture - Talk
Advanced web application architecture - TalkAdvanced web application architecture - Talk
Advanced web application architecture - Talk
 
DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...
DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...
DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...
 
Layers, ports and adapters
Layers, ports and adaptersLayers, ports and adapters
Layers, ports and adapters
 
Beyond design principles and patterns (muCon 2019 edition)
Beyond design principles and patterns (muCon 2019 edition)Beyond design principles and patterns (muCon 2019 edition)
Beyond design principles and patterns (muCon 2019 edition)
 
Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Brutal refactoring, lying code, the Churn, and other emotional stories from L...Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Brutal refactoring, lying code, the Churn, and other emotional stories from L...
 
Advanced web application architecture Way2Web
Advanced web application architecture Way2WebAdvanced web application architecture Way2Web
Advanced web application architecture Way2Web
 
Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Brutal refactoring, lying code, the Churn, and other emotional stories from L...Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Brutal refactoring, lying code, the Churn, and other emotional stories from L...
 
Beyond Design Principles and Patterns
Beyond Design Principles and PatternsBeyond Design Principles and Patterns
Beyond Design Principles and Patterns
 
Building Autonomous Services
Building Autonomous ServicesBuilding Autonomous Services
Building Autonomous Services
 
Advanced Application Architecture Symfony Live Berlin 2018
Advanced Application Architecture Symfony Live Berlin 2018Advanced Application Architecture Symfony Live Berlin 2018
Advanced Application Architecture Symfony Live Berlin 2018
 
Designing for Autonomy
Designing for AutonomyDesigning for Autonomy
Designing for Autonomy
 
Docker workshop
Docker workshopDocker workshop
Docker workshop
 
Docker swarm workshop
Docker swarm workshopDocker swarm workshop
Docker swarm workshop
 
Docker compose workshop
Docker compose workshopDocker compose workshop
Docker compose workshop
 
Building autonomous services
Building autonomous servicesBuilding autonomous services
Building autonomous services
 

Recently uploaded

Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 

Recently uploaded (20)

Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 

The Naked Bundle - Symfony Usergroup Belgium

  • 1. The Naked Bundle Matthias Noback @matthiasnoback Symfony Usergroup Belgium October 13th, 2014
  • 2. What's it all about?
  • 4. I could've called it BundleLitetm The No Code Bundle The Clean Bundle
  • 5. But “naked” was catchy and controversial
  • 7. The official view on bundles
  • 8. First-class citizens Documentation » The Quick Tour » The Architecture
  • 9. Importance Your code is more important than the framework, which is an implementation detail
  • 10. Reuse
  • 11. Nice!
  • 12. All your code lives in a bundle Documentation » The Book » Creating Pages in Symfony2
  • 13. Reuse “All your code in a bundle” contradicts the promise of reuse
  • 14. Everything lives inside a bundle Documentation » Glossary
  • 15. Not really true Many things live inside libraries (the Symfony components are libraries too!)
  • 17. But you probably know that already
  • 19. What about... ● Controllers ● Entities ● Templates ● ...
  • 20. They just need to be in a bundle Or do they?
  • 21. Don't get me wrong I love Symfony!
  • 22. But a framework is just a framework ● Quickstarter for your projects ● Prevents and solves big security issues for you ● Has a community you can rely on
  • 23. A framework is there for you
  • 24. Your code doesn't need a framework
  • 25. Noback's Principle Code shouldn't rely on something it doesn't truly need
  • 26. Bundle conventions Things in a bundle often rely on conventions to work
  • 28. So according to Noback's Principle, code shouldn't rely on bundle conventions too
  • 29. Naming conventions Controllers: ● *Controller classes ● *action methods Templates: ● in /Resources/views ● name: Controller/Action.html.twig
  • 30. Structural conventions Controller: ● Extends framework Controller class ● Is ContainerAware
  • 31. Behavioral conventions Controller: ● Is allowed to return an array ● Actions can type-hint to objects which will be fetched based on route parameters (??)
  • 32. Configuration conventions Use lots of annotations! /** * @Route("/{id}") * @Method("GET") * @ParamConverter("post", class="SensioBlogBundle:Post") * @Template("SensioBlogBundle:Annot:show.html.twig") * @Cache(smaxage="15", lastmodified="post.getUpdatedAt()") * @Security("has_role('ROLE_ADMIN')") */ public function showAction(Post $post) { }
  • 33. These conventions are what makes an application a Symfony2 application
  • 34. A Year With Symfony
  • 36. A bundle exposes resources
  • 37. Resources ● Service definitions ● Controllers ● Routes ● Templates ● Entities ● Form types ● Event listeners ● Translations ● ...
  • 38. No need for them to be inside a bundle
  • 39. When placed outside the bundle the resources could be reused separately
  • 40. The bundle would be really small
  • 41. And could just as well be a: Laravel package, Zend or Drupal module, CakePHP plugin, ...
  • 42. So the challenge is to Make the bundle as clean as possible
  • 43. Move the “misplaced” things to ● a library ● with dependencies ● but not symfony/framework­bundle ;)
  • 45. Reuse within the Symfony family Think: Silex, Laravel, etc.
  • 46. Allowed dependency HttpFoundation ● Request ● Response ● Exceptions ● etc.
  • 47. What do we rely on HttpKernel namespace SymfonyComponentHttpKernel; use SymfonyComponentHttpFoundationRequest; use SymfonyComponentHttpFoundationResponse; interface HttpKernelInterface { /** * Handles a Request to convert it to a Response. */ public function handle(Request $request, ...); }
  • 48. Why? My secret missions “Let's rebuild the application, but this time we use Zend4 instead of Symfony2”
  • 49. And of course Education
  • 50. You need a strong coupling radar
  • 51. Explicit dependencies ● Function calls ● Imported classes (“use”) ● Included files ● ...
  • 52. Implicit dependencies ● File locations ● File, class, method names ● Structure of return values ● ...
  • 54. use SymfonyBundleFrameworkBundleControllerController; use SensioBundleFrameworkExtraBundleConfigurationRoute; use SensioBundleFrameworkExtraBundleConfigurationTemplate; /** * @Route(“/article”) */ class ArticleController extends Controller { /** * @Route(“/edit”) * @Template() */ function editAction(...) { ... } } Controller
  • 55. TODO ✔ Don't rely on things that may not be there in another context: ✔ Parent Controller class ✔ Routing, template, annotations, etc.
  • 56. class ArticleController { function editAction(...) { ... } } Nice and clean ;)
  • 57. use SymfonyComponentHttpFoundationRequest; class ArticleController { public function editAction(Request $request) { $em = $this­> get('doctrine')­> getManager(); ... if (...) { throw $this­> createNotFoundException(); } ... return array( 'form' => $form­> createView() ); } } Zooming in a bit
  • 58. TODO ✔ Inject dependencies ✔ Don't use helper methods ✔ Render the template manually ✔ Keep using Request (not really a TODO)
  • 59. use DoctrineORMEntityManager; class ArticleController { function __construct( EntityManager $em, ) { $this­> em = $em; } ... } Inject dependencies
  • 60. Inline helper methods use SymfonyComponentHttpKernelExceptionNotFoundHttpException class ArticleController { ... public function newAction(...) { ... throw new NotFoundHttpException(); ... } }
  • 61. use SymfonyComponentHttpFoundationResponse; use SymfonyComponentTemplatingEngineInterface; class ArticleController { function __construct(..., EngineInterface $templating) { } public function newAction(...) { ... return new Response( $this­> templating­> render( '@MyBundle:Article:new.html.twig', array( 'form' => $form­> createView() ) ) ); } } Render the template manually
  • 62. Dependencies are explicit now Also: no mention of a “bundle” anywhere! use SymfonyComponentHttpFoundationRequest; use SymfonyComponentHttpFoundationResponse; use SymfonyComponentTemplatingEngineInterface; use DoctrineORMEntityManager;
  • 63. Dependency overflow use SymfonyComponentHttpFoundationResponse; use SymfonyComponentTemplatingEngineInterface; class ArticleController { function __construct( EntityManager $entityManager, EngineInterface $templating, TranslatorInterface $translator, ValidatorInterface $validator, Swift_Mailer $mailer, RouterInterface $router ) { ... } ... }
  • 65. One controller, many actions one action!
  • 66. __invoke() namespace MyLibraryControllerArticle; class New { function __construct(...) { // only what's necessary for this “action” } public function __invoke(...) { ... } }
  • 67. Nice! └─Controller └─Article ├─New.php ├─Edit.php ├─Archive.php └─Delete.php
  • 68. TODO ✔ Set up routing ✔ Create a service and provide the right arguments
  • 69. Bundle stuff: services.xml <!­­in MyBundle/Resources/config/services.xml → <?xml version="1.0" ?> <container> <services> <service id="new_article_controller" class="MyBundleControllerArticleNew"> <argument type="service" id="doctrine.orm.default_entity_manager" /> <argument type="service" id="templating" /> </service> </services> </container>
  • 70. Bundle stuff: routing.xml <!­­in MyBundle/Resources/config/routing.xml → <?xml version="1.0" encoding="UTF­8" ?> <routes> <route id="new_article" path="/article/new"> <default key="_controller"> new_article_controller:__invoke </default> </route> </routes> Pull request by Kevin Bond allows you to leave out the “:__invoke” part!
  • 71. Controller – Achievements ● Can be anywhere ● No need to follow naming conventions (“*Controller”, “*action”) ● Dependency injection, no service location ● Reusable in any application using HttpFoundation
  • 73. namespace MyBundleEntity; use DoctrineORMMapping as ORM; class Article { ... /** * @ORMColumn(type=”string”) */ private $title; } Entity conventions
  • 74. What's wrong with annotations? namespace MyBundleEntity; use DoctrineORMMapping as ORM; class Article { ... /** * @ORMColumn(type=”string”) */ private $title; }
  • 76. use DoctrineCommonAnnotationsAnnotationReader; $reader = new AnnotationReader(); $class = new ReflectionClass('Article'); $reader­> getClassAnnotations($class); BANG Class DoctrineORMMappingColumn not found
  • 77. Well, uhm, yes, but...
  • 78. Are you ever going to use anything else than Doctrine ORM?
  • 79. Well... Think about Doctrine MongoDB ODM, Doctrine CouchDB ODM, etc. namespace MyBundleEntity; use DoctrineORMMapping as ORM; use DoctrineODMMongoDBMappingAnnotations as MongoDB; use DoctrineODMCouchDBMappingAnnotations as CoucheDB; class Article { /** * @ORMColumn * @MognoDBField * @CoucheDBField */ private $title; }
  • 80. TODO ✔ Remove annotations ✔ Find another way to map the data
  • 81. namespace MyBundleEntity; class Article { private $id; private $title; } Nice and clean A true POPO, the ideal of the data mapper pattern
  • 82. Use XML for mapping metadata <doctrine­mapping> <entity name=”MyBundleEntityArticle”> <id name="id" type="integer" column="id"> <generator strategy="AUTO"/> </id> <field name=”title” type=”string”> </entity> </doctrine­mapping>
  • 83. Conventions for XML metadata ● For MyBundleEntityArticle ● Put XML here: @MyBundle/Resources/config/doctrine/ Article.orm.xml
  • 84. We don't want it in the bundle! There's a nice little trick
  • 85. You need DoctrineBundle >=1.2 { "require": { ..., "doctrine/doctrine­bundle": "~1.2@dev" } }
  • 86. use DoctrineBundleDoctrineBundleDependencyInjectionCompiler DoctrineOrmMappingsPass; class MyBundle extends Bundle { public function build(ContainerBuilder $container) { $container­> addCompilerPass( $this­> buildMappingCompilerPass() ); } private function buildMappingCompilerPass() { $xmlPath = '%kernel.root_dir%/../src/MyLibrary/Doctrine'; $namespacePrefix = 'MyLibraryModel'; return DoctrineOrmMappingsPass::createXmlMappingDriver( array($xmlPath => $namespacePrefix) ); } }
  • 87. Now: ● For MyLibraryModelArticle ● Put XML here: src/MyLibrary/Doctrine/Article.orm.xml
  • 88. Entities - Achievements ● Entity classes can be anywhere ● Mapping metadata can be anywhere and in different formats ● Entities are true POPOs
  • 90. Conventions ● In /Resources/views/[Controller] ● Filename: [Action].[format].[engine]
  • 91. The difficulty with templates They can have all kinds of implicit dependencies: ● global variables, e.g. {{ app.request }} ● functions, e.g. {{ path(...) }} ● parent templates, e.g. {% extends “::base.html.twig” %}
  • 92. Still, we want them out! And it's possible
  • 93. Documentation » The Cookbook » Templating » How to use and Register namespaced Twig Paths # in config.yml twig: ... paths: Twig namespaces "%kernel.root_dir%/../src/MyLibrary/Views": MyLibrary // in the controller return $this­> templating­> render('@MyLibrary/Template.html.twig');
  • 94. Get rid of absolute paths Using Puli, created by Bernhard Schüssek (Symfony Forms, Validation)
  • 95. What Puli does Find the absolute paths of resources in a project
  • 96. use WebmozartPuliRepositoryResourceRepository; $repo = new ResourceRepository(); $repo­> add('/my­library/ views', '/absolute/path/to/views/*'); /my-library/views /index.html.twig /absolute/path/to/views /index.html.twig echo $repo ­> get('/my­library/ views/index.html.twig') ­> getRealPath(); // => /absolute/path/to/views/index.html.twig
  • 97. Register “prefixes” Manually, or using the Puli Composer plugin // in the composer.json file of a package or project { "extra": { "resources": { "/my­library/ views": "src/MyLibrary/Views" } } }
  • 98. Twig templates // in composer.json { "extra": { "resources": { "/my­library/ views": "src/MyLibrary/Views" } } } Puli Twig extension // in the controller return $this­> templating ­> render('/my­library/ views/index.html.twig');
  • 99. Many possibilities ● Templates ● Translation files ●Mapping metadata ● Service definitions ● And so on!
  • 100. The future is bright ● Puli is not stable yet ● But I expect much from it:
  • 101. Puli will be the ultimate tool
  • 102. to create NAKED BUNDLES
  • 103. and to enable reuse of many kinds of resources
  • 104. not limited by project, framework, even language boundaries!
  • 105. But even without Puli There's a whole lot you can do to make your code not rely on the framework
  • 106. Remember The framework is for you Your code doesn't need it
  • 108. Buy it for $ 17,50 http://leanpub.com/a-year-with-symfony/c/symfony-usergroup-belgium
  • 109. Get a $10,00 introduction discount: http://leanpub.com/principles-of-php-package-design /c/symfony-usergroup-belgium
  • 110. Thank you Feedback: meetup.com Talk to me: @matthiasnoback
  • 111. Images Sally MacKenzie: www.amazon.com