Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Extending Symfony2 Web Application Framework
Extending Symfony2 Web Application Framework

Extending Symfony2 Web Application Framework: Symfony2 took the great features of the original framework to new levels of extensibility. With this practical guide you'll learn how to make the most of Symfony2 through controlling your code and sharing it more widely.

eBook
$9.99 $22.99
Paperback
$38.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Extending Symfony2 Web Application Framework

Chapter 2. Commands and Templates

In this chapter, we will review two of the most common kinds of extensions that you will encounter while working on a Symfony project:

  • Commands: They are similar to the ones that Symfony brings you, such as the ones already in the framework (cache:clear, doctrine:database:create, and so on)

  • Twig: It's relatively easy to extend the templating language of Symfony as well

Commands


Symfony ships with a powerful console component. Just like many components in Symfony, it can also be used as a standalone component to create command-line programs. In fact, Composer (http://getcomposer.org), the dependency manager that you use every day with Symfony, has its command line-based on the Symfony Console component.

Let's find out how to create commands and what they are good for.

The initial situation

Our site users have a profile on the website. On their profile, they can upload their own picture (in any avatar). They can upload any kind of picture with different sizes and ratios, and the system will crop it and/or resize it to a square picture of 150 x 150 pixels. We always keep the higher resolution uploaded picture but pregenerate the 150-pixel one to improve the load speed of our site. Now that so many people are browsing our site from very high resolution tablets, we need to make that profile picture also available in 300 pixels size.

This is a relatively heavy task...

Twig


By default, Symfony ships with the Twig templating system. Twig is incredibly powerful and out of the box. The possibilities offered by blocks, extending templates, including templates, and macros are huge and will be enough for most cases. There are cases where you still need something more though, and an extension for Twig is the only elegant way of doing so.

Twig offers five different ways to create extensions:

  • Globals: This lets you define some global variables that are available in all templates. You could access them like any other variable.

  • Functions: This will let you write {{my_function(var)}}.

  • Tests: These are specific functions that return Boolean values and can be used after the is keyword in templates.

  • Filters: They modify the output of an output tag.

  • Tags: This will let you define custom Twig tags.

Some of the pages on our website will require some JavaScript in them to make them a bit more dynamic or simple to use. The form to create a meetup for organizers will definitely use...

Summary


With commands, we can now easily create tools for the developers who work on our application. We know that commands have access to the whole service container. We also know how to make them rely as much as possible on services, making the code for the command available to the whole application, if we need it later.

We only saw one form of extensions for templates but know that all other extension types (with the exception of custom tags) are just as easy and straightforward to implement. Custom tags are quite complex, and they are also very rarely needed. You can learn the basics of creating a new tag at http://twig.sensiolabs.org/doc/advanced.html#tags.

Left arrow icon Right arrow icon

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 25, 2014
Length: 140 pages
Edition :
Language : English
ISBN-13 : 9781783287208
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Mar 25, 2014
Length: 140 pages
Edition :
Language : English
ISBN-13 : 9781783287208
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 104.97
Persistence in PHP with Doctrine ORM
$32.99
Extending Symfony2 Web Application Framework
$38.99
Symfony2 Essentials
$32.99
Total $ 104.97 Stars icon
Banner background image

Table of Contents

6 Chapters
Services and Listeners Chevron down icon Chevron up icon
Commands and Templates Chevron down icon Chevron up icon
Forms Chevron down icon Chevron up icon
Security Chevron down icon Chevron up icon
Doctrine Chevron down icon Chevron up icon
Sharing Your Extensions Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8
(8 Ratings)
5 star 37.5%
4 star 25%
3 star 25%
2 star 0%
1 star 12.5%
Filter icon Filter
Top Reviews

Filter reviews by




Richard R Perez Dec 07, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Is a short and concise book with clear goals. There are some concepts that even I heard before, I thought they was complicate, but with this book I realized that they are very simple.You need to know about Symfony2 is not a beginner book and also you won't learn everything about Symfony2 is a book that teach you how you can create your bundles in a way that they could be reused in other applications and in they way of it, it explains functionalities that can be helpful even if that is not your intention.
Amazon Verified review Amazon
xlthlx May 29, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is not for beginners, to understand and appreciate it you must have made at least one project with Symphony2.The book presents code examples mainly, very clear, that can be safely reused for your own projects.The ultimate goal of the book is to create and distribute your bundle, but each chapter can be considered separately, and each can help a lot to improve the way to implement functionality in Symfony2.I especially enjoyed the chapter on Commands and how to set them as an interface to Services, because, as written by the author, opening a terminal is already a technical operation for many people, and having a web interface for starting the process that the site admins could use is very powerful.The other chapters that I liked were the one on Doctrine, on how to use with MongoDB and coordinates, and the one on Twig and how to implement a widget for a map.I recommend this book to anyone who wants to deepen and improve his knowledge of Symfony2, and who wants to better understand the logic.
Amazon Verified review Amazon
Johnny B Mar 28, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I have been programming in PHP semi-professionally for 14 years now. I've done a few small and unpublished Symfony2 projects trying to learn it but never have felt like I was REALLY grasping it. I've found the Yii framework to be easier to use for someone who doesn't program every day as Yii is straight forward and you can dive right in with little refresher. Symfony has made me feel like I'm becoming a dinosaur in the programming world but at the same time I need to get caught up. I'm glad to say that this book is clear and concise (much more so than the Symfony book). It's very easy to follow along and has great examples that you can expand on. Within a few minutes of starting this book I could actually feel the cobwebs in my head clear and could grasp Symfony a whole lot easier. I can see myself jumping into Symfony projects just as easily as Yii and with the extra power and resources that Symfony has. If you are a seasoned PHP developer but having a difficult time grasping Symfony, this book will help you immensely. Thanks to everyone who made this book possible.
Amazon Verified review Amazon
Greg Freeman Jun 30, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
I've been using symfony2 since its release on numerous projects and I recently read through this book.If you are a beginner or just starting out with symfony2, I suggest you spend your time reading the documentation on symfony.com and working on some test projects first. This book is not beginner friendly and assumes you have a working knowledge of symfony2, some of the explanations are skipped over quickly which is great for the intended audience of the book, as you spend more time of more advanced uses of the framework.If you are an intermediate or more advanced user of symfony2, I am sure you will learn something by reading this book and it is well worth your time. Most books are very general, this book gives some real-world usage tips that you can use in your projects right now.This book has useful information that will
Amazon Verified review Amazon
Stan Jun 12, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Extending Symnfony by Sébastien Armand is a tutorial-style introduction to a variety of the ways that you can extend a Symfony 2 full stack installation. I’m a big fan of Symfony 2 and I’ve done a fair amount of app building with it, so I was interested in Armand’s book and seeing what new things I could discover about hooking into sf2.The book is filled with code samples, far more than you’ll find in most other technical books. Most of these code samples are also complete, which anyone who has traversed the official Symfony 2 cookbooks will greatly appreciate. Unfortunately, these code samples sometimes cross pages in inconvenient ways, and none of them include syntax highlighting which can make it hard to read at times.Armand tackles six (sort of eight) areas of Symfony 2 development where developers can tap in and extend existing functionality of Symfony 2. First and foremost he kicks off his tutorials with covering service definitions and listeners. These topics seem like they could have been separate chapters to me, but nonetheless he does a good job of giving real world examples of how to tie these things in. He especially does well with event listeners - the secret weapon of the Symfony 2 stack (in my opinion anyhow).Armand’s approach to extending symfony is project-based, meaning that through the book you’re working on building an app that handles some details for meet ups between users. You can think of it like the old Symfony 1 Askeet tutorial. This is a huge advantage of Armand’s book over other Symfony 2 texts you’ll find in the wild. Actual applications create context and drive home the concepts. As an added bonus, in this book you are NOT building yet another task manager!The Security chapter covers some of the more difficult areas of Symfony 2. Anyone who has dealt with Security in sf2 knows that, while extremely powerful, it can also be extremely challenging. Armand’s examples are helpful, especially as he tackles an OAuth implementation. Armand uses the Friends of Symfony UserBundle to get going, but unfortunately didn’t cover with too much depth getting started with this super handy bundle. The examples in the book are priceless, but I look forward to future revisions that cover the new SimpleAuth implementation in Symfony 2. The only other thing I wished Armand would have covered was securing an api with tokens and a custom user provider for doing this. He shows how a cookie can be used with an event listener, but truthfully there are better ways of tackling this problem in Symfony 2 that are more consistent with its security model.One of the most valuable chapters in this book is the Doctrine chapter. Doctrine 2’s official documentation lacks a lot of context. By being a project-based tutorial, Armand actually shows you how to write a custom data type, custom DQL function, and a custom filter, rather than stumble through the Doctrine 2 docs and hope you got close. This chapter in and of itself is a valuable resource for those times when you need to do these things.The final chapter discusses bundles briefly. This is one area of the book I felt could have been fleshed out a bit more. Armand covers the basics, but part of me felt like this chapter almost belonged at the beginning of the book instead of the tail end. The other thing that was missing from this chapter was bundle inheritance which, while a tricky subject, is a huge part of extending a Symfony 2 application.All in all I think this is a solid book on tapping into some of the more powerful features of Symfony 2 and it’s counterpart Doctrine 2. The book is at times a little oddly organized, but the code samples and tip are worthy any web developers time. If you’re looking to dive into some of the things in the book’s table of contents get yourself a copy and profit from Armand’s tutorials and extensive code samples.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.