57
My Symfony WTF

Sf2 wtf

Embed Size (px)

DESCRIPTION

Symfony2 è sicuramente uno dei framework migliori in circolazione, ma non sono tutte rose e fiori, soprattutto per chi inizia a sviluppare ed è alle prime armi. In questa presentazione vorrei condividere la mie esperienza di apprendimento ed utilizzo del framework, cercando di mettere in evidenza i miei momenti wtf e alcune linee guida per sviluppare applicazioni manutenibili

Citation preview

Page 1: Sf2 wtf

My Symfony WTF

Page 2: Sf2 wtf

Who am I?

Michele Orselli

CTO@Ideato @[email protected]

Page 3: Sf2 wtf

why a talk on WTF?

Why a talk on WTF?

Page 4: Sf2 wtf

why a talk on WTF?

I ♥

Page 5: Sf2 wtf

why a talk on WTF?

but...

Page 6: Sf2 wtf

why a talk on WTF?

Page 7: Sf2 wtf

why a talk on WTF?

Page 8: Sf2 wtf

why a talk on WTF?

Page 9: Sf2 wtf

why a talk on WTF?

Page 10: Sf2 wtf

the (old) times?

symfony 1 was focused onRapid Application Development

Page 11: Sf2 wtf

the shiny new thing

Symfony2 is focused on Application Development

Page 12: Sf2 wtf

the shiny new thing

Page 13: Sf2 wtf

the shiny new thing

Page 14: Sf2 wtf

the shiny new thing

Page 15: Sf2 wtf

speed as selling point

Page 16: Sf2 wtf

installation

create-project symfony/framework-standard-edition ./foo 2.3.0

Page 17: Sf2 wtf

cache/log permissions

Page 18: Sf2 wtf

cache/log permissions

Page 19: Sf2 wtf

cache/log permissions

php app/console cache:clear

Page 20: Sf2 wtf

cache/log permissions

Page 21: Sf2 wtf

cache all the things (in dev too)!

Page 22: Sf2 wtf

cache all the things (in dev too)!

http://symfony.com/doc/current/book/templating.html

Page 23: Sf2 wtf

user session in cache!

config.yml:

twig: cache: false

Page 24: Sf2 wtf

uhm, wait a minute...

Page 25: Sf2 wtf

user session in cache!

Page 26: Sf2 wtf

user session in cache!

capifony?

idephix? :-P

Page 27: Sf2 wtf

user session in cache!

config.yml:

framework: session: save_path: "%kernel.root_dir%/sessions/"

Page 28: Sf2 wtf

sf2 in da cloud

Page 29: Sf2 wtf

sf2 in da cloud

Page 30: Sf2 wtf

sf2 in da cloud

http://www.ideato.it/planet-ideato/symfony2-su-google-app-engine/

symfony2 needs a writable filesystemsymfony2 uses some restricted functionsyou can warmup cache, but paths are absolutethe problem is not only limited to symfony (eg: assetic)

Page 31: Sf2 wtf

sf2 in da cloud

Page 32: Sf2 wtf

annotations

/** * @ORM\Entity(repositoryClass="Ideato\OfferBundle\Repository\CityRepository") * @ORM\Table(name="city") */class City{ /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id;

/** * @ORM\Column(type="string", length=255) * @Assert\NotBlank() */ private $name;

Page 33: Sf2 wtf

annotations

use Doctrine\ORM\Mapping as ORM;use Symfony\Component\Validator\Constraints as Assert;

/** * @ORM\Entity(repositoryClass="Ideato\OfferBundle\Repository\CityRepository") * @ORM\Table(name="city") */class City{ /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id;

/** * @ORM\Column(type="string", length=255) * @Assert\NotBlank() */ private $name;

Page 34: Sf2 wtf

do you need everything?

@ParamConverter

Page 35: Sf2 wtf

do you need everything?

/** * @ParamConverter("post", class="SensioBlogBundle:Post") */public function showAction(Post $post){ ...}

Page 36: Sf2 wtf

do you need everything?

/** * @ParamConverter("post", class="SensioBlogBundle:Post") */public function showAction(Post $post){ ...}

public function showAction(Post $post){ ...}

Page 37: Sf2 wtf

do you need everything?

/** * @ParamConverter("post", class="SensioBlogBundle:Post") */public function showAction(Post $post){ ...}

public function showAction(Post $post){ ...}

public function showAction(Post $post, Comment $comment){ ...}

Page 38: Sf2 wtf

kill the magic?

/** * @ParamConverter("post", class="SensioBlogBundle:Post") */public function showAction(Post $post){ ...}

public function showAction(Post $post){ ...}

public function showAction(Post $post, Comment $comment){ ...}

Page 39: Sf2 wtf

explicit vs implicit

public function showAction($post_id){ $post = $this->getRepository(‘Posts’)

->findOneById($post_id);}

Page 40: Sf2 wtf

if u can it doesn’t mean you have to

use Doctrine\DBAL\Types\Type;use Doctrine\DBAL\Platforms\AbstractPlatform; /** * NewObject datatype */class NewObjectType extends Type{ public function convertToPHPValue($value, AbstractPlatform $platform) { $listeners = $platform -> getEventManager() -> getListeners('getContainer');  $listener = array_shift($listeners); $container = $listener -> getContainer();  return $container -> get('service'); }}

Page 41: Sf2 wtf

passing around the DIC is bad

class A{ protected $first_service; protected $second_service;

public function __construct(ContainerInterface $container) { $this->first_service = $container->get(‘firstService’); $this->second_service = $container->get(‘secondService’); }}

Page 42: Sf2 wtf

passing around the DIC is bad

class A{ protected $first_service; protected $second_service;

public function __construct(FirstService $first, SecondService $second) { $this->first_service = $first; $this->second_service = $second; }}

in services.yml:

services: my.service: arguments: - "@first_service" - "@second_service" class: My\Class\A

Page 43: Sf2 wtf

everything is a bundle

Page 44: Sf2 wtf

configuration (Mopa)...

mopa_bootstrap: version: ~ form: templating: MopaBootstrapBundle:Form:fields.html.twig horizontal_label_class: col-lg-3 horizontal_input_wrapper_class: col-lg-9 row_wrapper_class: form-group render_fieldset: true render_collection_item: true show_legend: true show_child_legend: false checkbox_label: both render_optional_text: true render_required_asterisk: false error_type: ~ tooltip: icon: icon-info-sign placement: top tabs: class: nav nav-tabs popover: icon: icon-info-sign placement: top collection: widget_remove_btn: attr: class: btn icon: ~ icon_color: ~ widget_add_btn: attr: class: btn icon: ~ icon_color: ~ navbar: template: MopaBootstrapBundle:Navbar:navbar.html.twig initializr: meta: title: MopaBootstrapBundle description: MopaBootstrapBundle keywords: MopaBootstrapBundle, Twitter Bootstrap, HTML5 Boilerplate author_name: My name author_url: # feed_atom: ~ feed_rss: ~ sitemap: ~ nofollow: false noindex: false dns_prefetch:

# Default: - //ajax.googleapis.com google: wt: ~ analytics: ~ diagnostic_mode: false

Page 45: Sf2 wtf

configuration (FosUserBundle)...

fos_user: db_driver: ~ # Required user_class: ~ # Required firewall_name: ~ # Required model_manager_name: ~ use_listener: true use_username_form_type: true from_email: address: [email protected] sender_name: webmaster profile: form: type: fos_user_profile name: fos_user_profile_form validation_groups:

# Defaults: - Profile - Default change_password: form: type: fos_user_change_password name: fos_user_change_password_form validation_groups:

# Defaults: - ChangePassword - Default registration: confirmation: enabled: false template: FOSUserBundle:Registration:email.txt.twig from_email: address: ~ # Required sender_name: ~ # Required form: type: fos_user_registration name: fos_user_registration_form validation_groups:

# Defaults: - Registration - Default

resetting: token_ttl: 86400 email: template: FOSUserBundle:Resetting:email.txt.twig from_email: address: ~ # Required sender_name: ~ # Required form: type: fos_user_resetting name: fos_user_resetting_form validation_groups:

# Defaults: - ResetPassword - Default service: mailer: fos_user.mailer.default email_canonicalizer: fos_user.util.canonicalizer.default token_generator: fos_user.util.token_generator.default username_canonicalizer: fos_user.util.canonicalizer.default user_manager: fos_user.user_manager.default template: engine: twig group: group_class: ~ # Required group_manager: fos_user.group_manager.default form: type: fos_user_group name: fos_user_group_form validation_groups:

# Defaults: - Registration - Default

Page 46: Sf2 wtf

configuration...

configuration is just another way of programming

Page 47: Sf2 wtf

configuration...

is there a way to create decoupled, maintainable code?

Page 48: Sf2 wtf

configuration...

libraries

Page 49: Sf2 wtf

ddd, code first, ...

src!"" Acme #"" CoreDomain $ !"" User $ #"" User.php $ #"" UserId.php $ !"" UserRepository.php !"" CoreDomainBundle #"" Repository $   !"" InMemoryUserRepository.php !"" AcmeCoreDomainBundle.php

http://williamdurand.fr/2013/08/07/ddd-with-symfony2-folder-structure-and-code-first/http://whitewashing.de

Page 50: Sf2 wtf

Wrap up

wrap up

Page 51: Sf2 wtf

Wrap up

after all Symfony2 is just aframework

Page 52: Sf2 wtf

Wrap up

framework != architecture

Page 53: Sf2 wtf

Wrap up

do you need that feature?

Page 54: Sf2 wtf

Wrap up

think in advance

Page 55: Sf2 wtf

Wrap up

keep things decoupled

Page 56: Sf2 wtf

that’s all folks!

Thank you!@_orso_ [email protected]

Page 57: Sf2 wtf

that’s all folks!

Pics Creditswtf per minute: http://www.codinghorror.com/blog/2009/02/whos-your-coding-buddy.htmlpills: http://www.daygame.com/2013/blog/swallowing-the-red-pill/umarell: http://www.informarexresistere.fr/clouds: http://www.flickr.com/photos/uncle_jerry/49341110/gae: http://venturebeat.files.wordpress.com/2013/05/google-app-engine-php-zend.jpg