Rendering a TWIG template without a header in Symfony is a task you’ll end up doing in your projects. You may also capture TWIG renders into strings before they get sent to the screen. If you render templates normally with the “render” method, you’ll see the headers output to your screen like so:

HTTP/1.0 200 OK Cache-Control: no-cache Date...

It’s as easy as changing “render()” to “renderView()”, this tells Symfony to skip the headers. This is handy:

... ->renderView('template'); // instead of render('template')

Sometimes you may need to modify the standard form type by customizing some of the form fields, adding form fields, changing the list of form choices, omitting certain input, and making other changes. It’s easier to alter an existing form type than duplicating it just to make a few modifications, which helps follow the DRY principle (Don’t Repeat Yourself!)

Symfony encourages, and makes it easy to stay with OOP (Object Oriented Principles) by keeping to DRY, so you not only write less code, you will have less to maintain. Not to mention your code is more reliable, which is why it is recommended to reuse form types and modify them as needed in different situations, just like you would other objects in your system.

Here are some more helpful Symfony form tips and snippets.

Set the form action inside buildForm():


public function buildForm(FormBuilderInterface $builder, array $options)
{
   $builder->setAction($path);
}

Change the forms “action” url path in the createForm():

$form = $this->createForm(new WhateverFormType(), $object, array(
          'action' => $this->generateUrl('action_route'),
));

 

Customize form values and attributes in the createFormBuilder():

$form = $this->createFormBuilder($task)
  ->setAction($this->generateUrl('target_route'))
  ->setMethod('POST')
  ->add('task', 'text')
  ->add('dueDate', 'date)
  ->add('save', 'submit')
  ->getForm();

From what I’ve gleaned from Symfony 2.5 documentation, repositories are best when they’re associated with a distinct entity and have a narrow use for that entity and its other related entities, these are logical associations even though all repositories are still reusable, even from other bundles.  Services are more global and easier to instantiate, not to mention that any dependencies of service objects can also be defined in the services.yml file so the Entity Manager or some other dependent classes can also be automatically injected as needed.

Also, the Symfony documentation suggests that any actions/queries contained in repositories should only retrieve information (act as a “getter”) and not alter any data, as opposed to classes called from a service where their utility is interchangeable as database level “getters” and “setters”.  That said, if you need objects that are easily called from within the controller where you may need to alter data, the Symfony documentation is a little thin on the “best practices”.  I think it’s so much easier to call on these actions/queries in the same repositories you’ve already instanced.

For more complex form projects in the past, I have preferred putting such “form handler” objects that need to actually alter data inside a “/Handlers” folder within the bundle’s “/Forms” folder in order to mirror the organization of “/Repositories” while differentiating the purposes of each, just to follow the “best practices” recommended by our friends at Sensio Labs.

For example, I create a new class that might be called upon to alter certain child entities, perhaps an entity that is a collection of entities for some complex form, like so:  “/MyBundle/Forms/Handlers/MainFormHandler.php”.  Then, within this “MainFormHandler.php” class, the Entity Manager is instanced and I can then call on different methods for the purpose of inserting and updating database data.  If you are PHP developer with Symfony experience, I would love to hear your take on repositories, services, and the special “Handler” classes I mention above.

– Aaron Belchamber

Senior Web Developer
Celebrating 20 years of evolutionary marketing


If you’ve designed your database and set it up, there’s a quick way to initialize your Symfony bundle to start talking to it within a minute! Here is where using a framework to do those mundane coding tasks empowers developers to focus their time and energy creating solutions and less time focusing on creating tools and setting up an environment to create those solutions.  After all, a lot of those tools are used in one form or another for many different projects, aren’t they?

As a developer, imagine if you have to keep writing some version of PHPMyAdmin over and over again, knowing that PHPMyAdmin does a better job.  That’s where using a PHP framework starts to really put muscle in any company’s web development team.  GET THEM ON A FRAMEWORK!  You’ll be happy you did later.

If you don’t have a YML file but have a table on your database, run this command to import YML file from your database. You can filter by a single table name, but be sure to use the camelCase names since this references Symfony’s objects, which are called Entities. This actually makes sense since as a coder you need to easily distinguish your database field names which usually incorporate underscores, from the class properties that also use class methods which use camelCase.

php app/console doctrine:mapping:import YourAppBundle yml –filter="Table"

Based on an YML File this command will create a single entity for you:

php app/console doctrine:generate:entities YourAppBundle:Table –path src/

And if you want to create all entities

php app/console doctrine:mapping:import YourAppBundle yml
php app/console doctrine:generate:entities YourAppBundle

If you are looking to learn more about the Symfony PHP framework, you can gain confidence watching a real pro slug through a few projects and pick up a lot of great, helpful tips along the way while chuckling at the real English he pervasively sprinkles with entertaining British anecdotes and self-outrage.  Chris, you’re really hard on yourself, and you are by no means a NOOB!  🙂

I finally made it over the Symfony hump and must admit that I wish I learned a PHP framework much sooner.  If you’re on the fence, learn one, trust me, it will be well worth your time.  If you’re stubbornly refusing to look into a PHP framework, the era of “cowboy coding” is over, you will be irrelevant in about 3 years unless you adapt.  Perhaps you should consider learning COBOL and specialize in migrating their COBOL code to web-based and bring them into 2003.  At least they’ll be in the same millennium!

http://mossco.co.uk/symfony-2/free-symfony-2-tutorial-videos/

Why use a PHP framework?

If you use jQuery, Prototype or Angular to speed up your Javascript development, PHPMyAdmin or MySQL Workbench to speed up database design and development, you are already using tools and frameworks to support your web development process.  These are tools probably already in your tool kit which help you accelerate development time by freeing you up to focus on writing more effective, better designed, and more intuitive code without worrying about the tools you need to accomplish those tasks.  Often, we need to use the same tools on other projects, and this interchangeability of the tools in frameworks is where you will achieve compounding returns each time you use it.  There’s a familiarity and understanding, a common ground everyone in your development team will start from.

You will find when you delve deeper into these frameworks that they save you a lot of time so the quality of your code and what it can do is better thought out, structured, ready for re-use, and the results will be exponentially better.  Learning a PHP framework is an investment to better collaborative development environments and smoother sailing for any company who depends on a website.

Future proof your web assets

Another big reason to consider moving your web department to a framework is future compatibility.  As your company grows its web assets, you may expand and find yourself in need of another web developer.  The younger developers may only understand and be effective through a framework.  Sure, the more experienced who don’t use frameworks may argue that they have written a few pseudo PHP frameworks in the past, but those libraries and tools they made were custom built.  Without standardization and a common experience and understanding, unlike many developers share with the real frameworks like Symfony, Zend, CakePHP, Laravel, CodeIgnitor, or Yii to name a few, your company will not gain the benefits these PHP frameworks inherently provide.  It’s compounding returns and with the exception of Zend, moving to a PHP framework will cost you only in time, but the trade off for the future will put you in a much stronger position with much stabler websites and code that will be written that will not only last longer, but be much more useful and effective to your company.  That means more profits with compounding returns and less waste, a pretty good combination, don’t you think?