Learning Symfony2 by practice


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Learning Symfony2 by practice

  1. 1. Learning Symfony by practice SYMFONY2 PHP Framework
  2. 2. About: I part: Symfony in theory ● Composer ● Symfony ECO system (twig, doctrine2, assetic) ● DI or what is symfony service container? ● Symfony configuration ● Routing ● Bundle and it's structure ● Symfony best practices discovered using symfony II part: Symfony in practice. Workshop. ● DoubleBlogBundle ● What we try in practice: – Bundle – Controller – Routing – Twig – Entities – Forms – Services and DI
  3. 3. What is SYMFONY2? ● Symfony2 is a reusable set of standalone, decoupled and cohesive PHP components. ● Symfony2 is made from components – bundles. ● Symfony2 is also a full-stack framework. ● Compliant with PSR-0 , PSR-1, PSR-2, PSR-3. ● Very active community ● Most popular PHP project on github.com (873 contributors)
  4. 4. Composer ● What is composer? Dependency Manager for PHP ● composer.json http://pastebin.com/iKRtNwq8 ● composer.lock ● https://packagist.org/ and http://knpbundles.com/ ● When use composer update and composer install commands?
  5. 5. Symfony ECO system ● Twig - the flexible, fast, and secure template engine for PHP ● Assetic – assets + filters. – Assets is all your application static content (css, js, images) – Filters (LESS, SASS or CoffeeScript) ● Doctrine 2 – is library for persisting and reading information from your database.
  6. 6. Twig template example
  7. 7. Dependency injection component ● What is DI? – is a software design pattern that allows the removal of hard-coded dependencies and makes it possible to change them, whether at run-time. Dependency injection means giving an object its instance variables. Really. That's it. ● What is Service container? – A Service Container (or dependency injection container) is simply a PHP object that manages the instantiation of services (i.e. objects). ● We will learn more in second part. ● http://knpuniversity.com/screencast/dependency-injection/services
  8. 8. Symfony configuration ● 1. Different formats for configuration: xml, yaml, ini, php, annotations ● 2. The application in general using Yaml files. ● 3. Routing using Yaml files or annotations. ● 4. Service definitions using XML files or yaml. ● 5. Documents and entities using annotations. ● 6. Documents and entity validation using annotations.
  9. 9. Routing acme_demo.article.show         path:/articles/{culture}/{year}/{title}.{_format}         defaults: { _controller: AcmeDemoBundle:Article:show, _format: html }         method: GET         requirements:                 culture: en|fr                 _format: html|rss                 year:    d+ ● Earlier routes always wins ● Use YAML format. ● Routing name – {name of the bundle without  “Bundle”}.{name of the  controller without “Controller”}. {name of the action without  “Action”} – acme_demo.article.show
  10. 10. Symfony application structure ● app:/ the application configuration ● src:/ the project PHP code ● web:/ web root directory, all static content ● vendors:/ the third-party dependencies
  11. 11. Bundle structure
  12. 12. Symfony2 best practices ● Keep controller slim  – controller code is not testable – all business logic goes to services ● To keep it slim – use services. – event listeners – Custom form handlers?  ● If you want to access global PHP variables like $_POST, $_GET, $_COOKIES values use Symfony  Request object. – Awoid use $request->get(‘key’).  – use $request->query->get(‘key’) or $request->request->get(‘key’) ● When choosing ids for services, use only underscores and lowercase characters. Optionally provide  namespacing using dots.  ● The service id for the service of class MailManager inside the MatthiasAccountBundle may be  matthias_account.mail_manager. ● Awoid inject Symfony service container. Just in special cases. ● Awoid inject Request and Security components. Just in special cases.
  13. 13. Symfony workshop ● DoubleCommentBundle ● Entities: Post, PostComment ● Form: PostFormType, PostFormType ● Bundle, Controller, Routing, Twig, Services
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.