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
AU$14.99 AU$42.99
Paperback
AU$53.99
Subscription
Free Trial
Renews at AU$24.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $24.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781783287192
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $24.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
AU$24.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
AU$249.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 AU$5 each
Feature tick icon Exclusive print discounts
AU$349.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 AU$5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total AU$ 145.97
Persistence in PHP with Doctrine ORM
AU$45.99
Extending Symfony2 Web Application Framework
AU$53.99
Symfony2 Essentials
AU$45.99
Total AU$ 145.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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.