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

Symfony3

 Symfony3

Freerich

May 20, 2015
Tweet

Other Decks in Technology

Transcript

  1. # Symfony 2.6 # app/config/config.yml
 framework:
 templating:
 assets_version: 'v5'
 assets_version_format:

    '%%s?version=%%s'
 assets_base_urls:
 http: ['http://cdn.example.com']
 ssl: ['https://secure.example.com']
 packages:
 # ...
  2. # Symfony 2.7
 # app/config/config.yml
 framework:
 assets:
 version: 'v5'
 version_format:

    '%%s?version=%%s'
 base_path: ~
 base_urls: - 'http://cdn.example.com' - 'https://sec.example.com'
 packages:
 # ...
  3. {# Symfony 2.6 #}
 {{ asset('logo.png', absolute = true) }}


    
 {# Symfony 2.7 #}
 {{ absolute_url(asset('logo.png')) }}
 
 
 {# Symfony 2.6 #}
 {{ asset('logo.png', version = 'v5') }}
 
 {# Symfony 2.7 (do nothing, version is automatically appended) #}
 {{ asset('logo.png') }}
 
 {# use the asset_version() function if you need to output it manually #}
 {{ asset_version('logo.png') }}
  4. // Symfony 2.6
 use Symfony\Component\OptionsResolver\OptionsResolverInterface;
 
 class TaskType extends AbstractType


    {
 // ...
 public function setDefaultOptions(OptionsResolverInterface $resolver)
 {
 $resolver->setDefaults(array(
 'data_class' => 'AppBundle\Entity\Task',
 ));
 }
 }
  5. // Symfony 2.7
 use Symfony\Component\OptionsResolver\OptionsResolver;
 
 class TaskType extends AbstractType


    {
 // ...
 public function configureOptions(OptionsResolver $resolver)
 {
 $resolver->setDefaults(array(
 'data_class' => 'AppBundle\Entity\Task',
 ));
 }
 }
  6. In order to to use type casting such as (string)

    $user and {{ user }} in Twig templates
  7. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 
 class DefaultController extends Controller
 {
 public function

    indexAction()
 {
 // Symfony 2.6
 $value = $this->container->getParameter('param_name');
 
 // Symfony 2.7
 $value = $this->getParameter('param_name');
 }
 }
  8. # app/config/routing.yml
 contact:
 path: /contact
 defaults: { _controller: AcmeDemoBundle:Main:contact }


    condition: "request.headers.get('User-Agent') matches '%allowed_user_agents%'"
  9. To be able to support PHP and Twig in Symfony,

    there is a huge abstraction layer.
  10. class ReferenceExample
 {
 public $info = "Circular and sibling references

    are displayed as `#number`.\nHovering them highlights all instances in the same dump.\n";
 }
 $var = new ReferenceExample();
 $var->aCircularReference = $var;
 dump($var);
  11. You are also able to display the debug output in

    the web debug toolbar {% dump variable1, variable2 %}
  12. // Symfony 2.5
 if (false === $this->get('security.context') ->isGranted('ROLE_ADMIN')) { ...

    }
 // Symfony 2.6
 if (false === $this ->get('security.authorization_checker')- >isGranted('ROLE_ADMIN')) { ... }
  13. // Symfony 2.5
 $user = new Acme\UserBundle\Entity\User();
 $factory = $this->container->get('security.encoder_factory');


    $encoder = $factory->getEncoder($user);
 $password = $encoder->encodePassword($plainTextPassword, $user->getSalt());
 
 // Symfony 2.6
 $user = new Acme\UserBundle\Entity\User();
 $encoder = $this->container->get('security.password_encoder');
 $password = $encoder->encodePassword($user, $plainTextPassword);
  14. // Symfony 2.5
 public function loginAction(Request $request)
 {
 $session =

    $request->getSession();
 
 if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
 $error = $request->attributes->get(
 SecurityContextInterface::AUTHENTICATION_ERROR
 );
 } elseif (null !== $session && $session->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
 $error = $session->get(SecurityContextInterface::AUTHENTICATION_ERROR);
 $session->remove(SecurityContextInterface::AUTHENTICATION_ERROR);
 } else {
 $error = '';
 }
 
 $lastUsername = (null === $session) ? '' : $session->get(SecurityContextInterface::LAST_USERNAME);
 
 return $this->render(
 'AcmeSecurityBundle:Security:login.html.twig',
 array(
 'last_username' => $lastUsername,
 'error' => $error,
 )
 );
 }
  15. // Symfony 2.6 public function loginAction()
 {
 $helper = $this->get('security.authentication_utils');


    
 return $this->render('login.html.twig', array(
 'last_username' => $helper->getLastUsername(),
 'error' => $helper->getLastAuthenticationError(),
 ));
 }
  16. use Symfony\Component\Validator\Constraints as Assert;
 
 class User
 {
 /**
 *

    @Assert\LessThan("-18 years")
 */
 protected $dateOfBirth;
 }
  17. use Symfony\Component\Validator\Constraints as Assert;
 
 class Event
 {
 /**
 *

    @Assert\Range(
 * min = "first day of January",
 * max = "first day of January next year"
 * )
 */
 protected $startDate;
 }
  18. // Symfony 2.6 return $this->redirectToRoute('homepage');
 return $this->redirectToRoute('product_show', array('id' => 12),

    301);
 
 // Previous Symfony versions
 return $this->redirect($this->generateUrl('homepage'));
 return $this->redirect($this->generateUrl('product_show', array('id' => 12)), 301);
  19. // Symfony 2.6
 $this->addFlash('info', 'The item was created successfully.');
 


    // Previous Symfony versions
 $this->get('session')->getFlashBag()->add( 'info', 'The item was created successfully.' );
  20. // Symfony 2.6
 if ($this->isGranted('ROLE_ADMIN')) {
 // ...
 }
 


    // Previous Symfony versions
 if ($this->get('security.context')- >isGranted('ROLE_ADMIN')) {
 // ...
 }
  21. // Symfony 2.6
 $this->denyAccessUnlessGranted( 'ROLE_EDIT', $item, 'You cannot edit this

    item.' );
 
 // Previous Symfony versions
 if (false === $this->get('security.context')->isGranted('ROLE_EDIT', $item)) {
 throw $this->createAccessDeniedException('You cannot edit this item.');
 }
  22. // Symfony 2.6
 $this->isCsrfTokenValid('token_id', 'TOKEN');
 
 // Previous Symfony versions


    use Symfony\Component\Security\Csrf\CsrfToken;
 $this->get('security.csrf.token_manager')->isTokenValid( new CsrfToken('token_id', 'TOKEN') );
  23. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
 $language = new ExpressionLanguage();
 
 echo $language->evaluate('1 +

    1');
 // will echo 2
 
 echo $language->compile('1 + 2');
 // will echo "(1 + 2)"
  24. /**
 * @Assert\Expression("this.getFoo() == 'fo'", message="Not good!" */ class Obj


    {
 public function getFoo()
 {
 return 'foo';
 } ...
  25. A new major version is "just" a milestone where we

    can clean things up and get rid of the cruft
  26. • "# app/
 $ "# cache/
 $ "# config/
 $

    "# logs/
 $ %# ...
 "# src/
 $ %# ...
 "# vendor/
 $ %# ...
 %# web/
 "# app.php
 %# ... "# app/
 $ "# AppCache.php | "# AppKernel.php | "# config/ | %# ... "# bin/ | "# console.php | %# ... "# src/
 $ %# ... "# var/ | "# cache/ | "# logs/ | %# ... "# web/
 | "# app.php
 | %# ... "# phpunit.xml.dist %# ... Old New
  27. Symfony 2.7, to be released in May 2015, will be

    the next long term support release
  28. – The Release Process The dual maintenance mode was adopted

    to make every Symfony user happy. Fast movers, who want to work with the latest and the greatest, use the standard version: a new version is published every six months, and there is a two months period to upgrade. Companies wanting more stability use the LTS versions: a new version is published every two years and there is a year to upgrade.
  29. Merge strategy • New features will be merged in 2.7

    if they do not break backward compatibility or if they do not impact existing code • New features will only be merged in 3.0 if there is no way to make them work in the 2.7 branch • Bug fixes will still be merged in the oldest and still maintained branch and will be merged back to newer versions on a regular basis, including the master (aka 3.0) branch.
  30. People still on Symfony 2.3 have a year (until May

    2016) to upgrade to Symfony 2.7
  31. This version will work as any other 2.x versions, we

    will be able to add new features, deprecated other ones, and provide a good upgrade path to Symfony 3.0
  32. Fin