Starting in Symfony2: Course 4 (2.4+)

UPGRADE! Check out the newest version of this tutorial

Welcome back for part 4 of our Starting in Symfony2 series! We've got some really important topics planned and we're going to start with mastering assets and the Assetic library. We'll bust cache, minimize our CSS & JS files and get you ready to do just about anything with your static assets. We'll also dive much deeper into form rendering - customizing our forms to work with Twitter Bootstrap and learning a lot more about the powerful and advanced mechanisms behind form theming. Finally, we'll cover deployment: what steps are always necessary and some options for automating the process. Let's do this!

Highlights:

  • The Twig asset function
  • More about Dependency Injection Parameters and the purpose of parameters.yml
  • Cache busting with assets_version and assets_version_format
  • Introduction to Assetic
  • Assetic filters, including cssrewrite, cssmin and the apply_to option
  • Dumping assets in the prod and dev environments and the secret of turning use_controllers to false
  • The basics of form customization and theming
  • FormView "variables" - what they are and how you can use them to do just about anything while customizing how a form renders
  • Using the finishView to control FormView "variables"
  • The steps and best practices for deployment with tips on automating the process

Your Guides

Ryan Weaver Leanna Pelham

Questions? Conversation?

  • 2015-01-21 weaverryan

    Congrats! And thanks :).

    Where to go? I have a few suggestions:

    1) Build something! This will of course create more questions

    2) Go through our Symfony Journey Screencast - it's all about mastering what's under the surface: https://knpuniversity.com/scre... (and there will be a few more added to this series). Also, check out the Symfony tag for anything else you haven't seen - we'll always be adding more: https://knpuniversity.com/scre...

    3) Checkout the Symfony Cookbook page and see what topics interest you - these are like blog posts aimed at specific topics: http://symfony.com/doc/current...

    If you have any other thoughts or ways we can be helpful at this stage, I'd love to hear them. It's been fun interacting with you along the way :).

    Cheers!

  • 2015-01-20 Diego Aguiar

    Hey there, great tutorials!
    Finally I made it, I give em 10/10

    I would like to know where should we go after completing this 4 episodes ?

  • 2014-11-19 weaverryan

    Hey Edison!

    Hmm, the "npm install uglifycss --save" *should* have created 2 things:

    1) a node_modules/uglifycss directory
    2) A node_module/.bin/uglifycss file

    *However*, I forgot before that these files in node_modules/.bin are created via symbolic links, so I don't know how this will work on windows. Quite possibly, these files will not be there. BUT, that's totally fine - they are just there for convenience. You should be able to point directly at node_module/uglifycss/uglifycss.

    Let me know if that path exists - that's the *true* uglify binary file.

    Cheers!

  • 2014-11-19 Edison

    Hello Ryan & KNPTeam,

    I have a question this time related on the installation of uglifycss.

    I'm running this project on Windows8, I've installed uglifycss successfully using the following command: npm install uglifycss --save...It worked, but it didn't install the Folder .bin/uglifycss and its binary file inside....The fact is that It broke the styles as the root on the config.yml is not correct.
    (
    uglifycss:
    bin: %kernel.root_dir%/../node_modules/.bin/uglifycss
    )

    I've been reading on the Cookbook and it suggested another way of installation using this command: npm install -g uglifycss, I did it and it installed a folder with name "g" and some other files.....Is there a way to get this .bin/uglifycss binary file from other way, or should I do extrawork somewhere??
    No Iddea....
    Thanks and Keep in touch!

  • 2014-09-19 Scott Gutman

    I bought all your videos, they are incredibly helpful.
    I have been struggling with some design concepts and placement of business logic. Trying to keep controllers thin, entities pure, repositories "queryized" only left unorganized DI classes. Then I saw ModelManagers and it clicked. This blog has the general idea, but I bet you guys could explain it crazy or go to the next level (and another video) http://problematic.io/2012/03/...

    Anyway, just a thought and thank you!

  • 2014-09-11 Elvism

    Maybe you can create a new set of Q&A screencast for this year with more tutorials on advanced topics. Will be waiting for them, thanks!

  • 2014-09-11 weaverryan

    That's really nice to hear and great feedback! We've been thinking about potentially making smaller, more focused tutorials (this would be a great example). The challenge is that our model is a per-video price, and I'm not sure if that works as well with smaller videos.

    Thanks!

  • 2014-09-11 Elvism

    The documentation in the Symfony website for sure is good. But I found this as an interesting topic to create a screencast that will accomplish more the huge topic of forms in symfony2. Also I have learned more Symfony2 watching your videos than reading the official documentation. I like more video tutorials to listen and see how is done :)

  • 2014-09-11 weaverryan

    Hey!

    We could! But I'll ask you first about this cookbook entry - http://symfony.com/doc/current... - have you found it useful? Are you trying to do something that this doesn't cover?

    Thanks!

  • 2014-09-11 Elvism

    Hi Ryan! Can be included on this course or maded a new separate screencast about field collections? http://symfony.com/doc/current...

  • 2014-08-22 weaverryan

    Hi there!

    We have no plans to, but I would love to hear what you like about udemy that you would like to see with our content!

    Thanks!

  • 2014-08-22 nulata

    are there any chances you guys be using udemy.com platform?

  • 2014-06-21 weaverryan

    Awesome! It all depends on whether or not you last installed the assets with a symlink or a copy. If you did it via a symlink, then you're good forever (you can add new stuff and it's just there). If it's via a copy, then you have to re-install. We do show using symlink in the video, BUT, by default, when you run "php composer.phar install", it runs assets:install for you, but uses the "copy" option (not symlink). So, you might have it symlinked at first, then composer changes it to a copy. Kind of a gotcha - something I'm hoping we'll improve in Symfony's core.

    Cheers!

  • 2014-06-21 Dizzy Bryan High

    Thanks Ryan, yes the problem was that i had not installed the assets, it wasn't mentioned in the video, to re-run that after adding bootstrap, now you say it it seems obvious!
    something i need to remember!

  • 2014-06-20 weaverryan

    Hey!

    Hmm, first, make sure you're "installing the assets", which creates the symlink (or copy if you're on Windows) from web/bundles/even to src/Yoda/EventBundle/Resources/public:

    php app/console assets:install --symlink

    (leave the --symlink off if you're in Windows). Without this, web/bundles/event isn't a valid path (and should cause the error!).

    Whether this is the problem or not, I'd actually open up the web/bundles/event directory in PHPStorm and make sure that the path (e.g. bundles/event/css/bootstrap/css/bootstrap.css) exists and is exactly correct.

    Let me know what you find out! :)

  • 2014-06-18 Dizzy Bryan High

    Hi there, very much enjoying this episode,

    I've got a problem with integrating bootstrap.
    I've extracted the bootstrap dir to EventBundle/public/css/
    The bootstrap directory and files are there as in the video, but for some reason, Assetic says:
    [error] The source file "D:/Ampps/www/s2.starwarsevents.local/app/../web/bundles/event/css/bootstrap/css/bootstrap.css" does not exist.

    also in PHPStorm it's marking 'bundles/event/css/bootstrap/css/bootstrap.css' in base.html.twig as 'missing asset'

    I tried the `Invalidate Cache / restart` command from PHPStorm, and the `missing asset` error disappears once PHPstorm restarts.

    But as soon as i run the `assetic:dump --watch --force` command it generates the same error and the `missing asset` appears in phpstorm again, any ideas as what could be amiss here?

  • 2014-03-12 Luis Lopes

    +1

  • 2014-01-22 weaverryan

    Thanks Thomas! The "has-error" is correct for Bootstrap 3 and the "error" is correct for Bootstrap 2, which we use in the screencast. Either way, it's a good note, and when we update the recording, we'll update the class names as well :).

    Thanks!

  • 2014-01-22 Thomas

    for bootstrap the class for the fields to highlight when there's an error is 'has-error' now instead of just 'error'. submitted a pull request :)

  • 2013-09-23 weaverryan

    I've added a note to look into this. Technically, Symfony shouldn't require the PHP Intl extension, though in practice it causes problems when you don't have this. I'll see if we can clarify this and then add some notes in some spots that should help out.

    Cheers!

  • 2013-09-23 weaverryan

    And hi again Nikos!

    Yes, the downloaded code was out of date, which I've fixed at https://github.com/knpuniversi... and deployed. The script *is* correct with code that works for Symfony 2.2 and higher. The screencast still shows the old code, but with a note to see the script on the site for the updated details. The idea is all still the same, but some function arguments changed in 2.2.

    And thanks for pointing this out as well :)

  • 2013-09-23 weaverryan

    Hi Nikos!

    You're right! The screencast had the correct paths, but the script on the site had a typo. I've fixed it at https://github.com/knpuniversi... and deployed it to the site.

    Thanks for pointing that out!

  • 2013-09-19 Nikos C.

    in "Assets" chapter you must change the paths from "events" to "event"

  • 2013-09-19 Nikos C.

    I think, the ExceptionController implementation in the source is not compatible with symfony versions >= 2.2.

    Current composer.lock has symfony 2.3.1

  • 2013-09-19 Nikos

    Before running the "php composer.phar install" don't forget to enable the PHP Internationalization Extension.

  • 2013-07-25 Raphael Schumacher

    +1 :-)

  • 2013-07-08 Diego Viola

    Full coverage of SonataAdminBundle would be very appreciated! :)

  • 2013-02-22 Alberto

    Hello Ryan & Leanna,
    and thank you for those videos, they've been very helpful!

    Can you make a video about SonataAdminBundle?

    Have a great weekend! :)

  • 2013-02-08 Almog Baku

    Cool! thanks..
    Do you know about third-party bundle who can merge they all into a one file, or maybe it avilable as theme system?
    Also I would really like to know about some recommended bundles who handle issues like this(in other words- common recomended bundles)..

    Thanks a lot!
    ~ AlmogBaku

  • 2013-02-07 weaverryan

    Hi Almog!

    To do that, you use the stylesheet or javascript tag exactly the same way as you normally use it in the base template. If you have 1 stylesheet tag in the base template and one in a child/sub template, they do not somehow mix - the end user will download 2 CSS files - 1 representing the combined files from the base template and 1 representing the combined files from the subtemplate. In other words, you add assetic CSS/JS to a child template simply by overriding the "stylesheets" or "javascripts" block and including an Assetic stylesheet or javascript tag, just like normal. Assetic now sees 2 stylesheet tags - one from each template - there's no magic mixing :).

    Thanks!

  • 2013-01-18 Almog Baku

    Hey! cool episode.. I was really enjoy..

    I have some question:
    what is the right way to add an "assetic object"(css/js file) by sub template(in the bundle view)?

  • 2012-12-23 Daniel

    Finally episode 4. What a great Xmas present.

    I hope you keep up the good work. I am looking forward to more episodes, preferably shorter and one-topic episodes.

  • 2012-07-27 Krishna

    thanks for the quality screen casts.
    I really wish that you create more screencasts related to the awesome symfony2 framework.
    I am interested to see a railscasts sort of thing for symfony which actually covers how to achieve/solve a specific task .

    example: .
    * using backbone in symfony2
    * covering third party bundles like RestBundle ,etc

    regards