Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Symfony 101

Osman
April 18, 2015

Symfony 101

Istanbul PHP Meetup

Osman

April 18, 2015
Tweet

More Decks by Osman

Other Decks in Technology

Transcript

  1. What is Symfony2 ? First, Symfony2 is a reusable set

    of standalone, decoupled, and cohesive PHP components that solve common web development problems. Then, based on these components, Symfony2 is also a full-stack web framework.
  2. What is Symfony2 ? Because I really don't care whether

    Symfony2 is MVC or not. Probably because the MVC word is so overloaded and because nobody implements exactly the same MVC pattern anyway. The separation of concerns is all I care about. And if you like to call Symfony2 an MVC framework, then you should know that Symfony2 is really about providing the tools for the Controller part, the View part, but not the Model part. It's up to you to create your model by hand or use any other tool, like an ORM. Of course, tight integration exists for the most well known ORMs like Doctrine2 and Propel; but they are optional dependencies. The Symfony2 core features do not and will never rely on any ORM. I don't like MVC because that's not how the web works. Symfony2 is an HTTP framework; it is a Request/Response framework. That's the big deal. The fundamental principles of Symfony2 are centered around the HTTP specification.
  3. Principles • Follow current standards • Follow best practices •

    Don’t reinvent the wheel • Do one thing, make it excellent • Decouple your code • Make each part is replaceable
  4. Components • Asset • BrowserKit • ClassLoader • Config •

    Console • CssSelector • Debug • DependencyInjection • DomCrawler • EventDispatcher • ExpressionLanguage • Filesystem • Finder • Form • HttpFoundation • HttpKernel • Intl • OptionsResolver • Process • PropertyAccess • Routing • Security • Serializer • Stopwatch • Templating • Translation • Validator • VarDumper • Yaml
  5. Projects using Symfony • Drupal • Laravel • phpBB •

    ez Publish • Joomla! • PHPUnit • Piwik • Sylius • Silex • Behat • Composer • Doctrine • Propel • Zikula
  6. Symfony Standard Edition Symfony distribution is made up of Symfony2

    components, a selection of bundles, a directory structure, a default configuration, and an optional Web configuration system. You can add or remove bundles, change the default configuration, or modify the file structure, etc., based on your needs and desires!
  7. Symfony Standard Edition An AppBundle you can use to start

    coding Twig as the only configured template engine Doctrine ORM/DBAL Swiftmailer Annotations enabled for everything FrameworkBundle SensioFrameworkExtraBundle DoctrineBundle TwigBundle SecurityBundle SwiftmailerBundle MonologBundle AsseticBundle WebProfilerBundle * SensioDistributionBundle * SensioGeneratorBundle *
  8. Installation With Symfony Installer $ curl -LsS http://symfony.com/installer > symfony.phar

    $ sudo mv symfony.phar /usr/local/bin/symfony $ chmod a+x /usr/local/bin/symfony With Composer $ composer create-project symfony/framework- standard-edition my_project_name
  9. Directory structure app/ This directory contains the application configuration. src/

    All the project PHP code is stored under this directory. vendor/ Any vendor libraries are placed here by convention. web/ This is the web root directory and contains any publicly accessible files.
  10. Running Symfony2 $ cd myproject/ $ php app/console server:run Point

    your browser to http://localhost:8000 You can also setup your Nginx or Apache to access to your Symfony2 installation from a virtual host.
  11. The Bundle System A bundle is similar to a plugin

    in other software, but even better. The key difference is that everything is a bundle in Symfony, including both the core framework functionality and the code written for your application. Bundles are first-class citizens in Symfony. This gives you the flexibility to use pre-built features packaged in third-party bundles or to distribute your own bundles. It makes it easy to pick and choose which features to enable in your application and to optimize them the way you want.
  12. The Bundle System A bundle is simply a structured set

    of files within a directory that implement a single feature. You might create a BlogBundle, a ForumBundle or a bundle for user management (many of these exist already as open source bundles). Each directory contains everything related to that feature, including PHP files, templates, stylesheets, JavaScripts, tests and anything else. Every aspect of a feature exists in a bundle and every feature lives in a bundle.
  13. Bundle structure Controller/ Contains the controllers of the bundle (e.g.

    RandomController.php). DependencyInjection/ Holds certain dependency injection extension classes, which may import service configuration, register compiler passes or more (this directory is not necessary). Resources/config/ Houses configuration, including routing configuration (e.g. routing.yml). Resources/views/ Holds templates organized by controller name (e.g. Hello/index.html.twig). Resources/public/ Contains web assets (images, stylesheets, etc) and is copied or symbolically linked into the project web/ directory via the assets:install console command. Tests/ Holds all tests for the bundle.
  14. Configuration All configuration is found under /app/config config.yml is the

    base of core configuration Extensible for different environments like config_test.yml It also imports parameters.yml, security.yml and services.yml security.yml holds configuration of SecurityBundle Routing configuration made through routing.yml Dependency Injection configuration imported from services.yml All bundles have it’s own services.yml and routing.yml*