Using the Symfony VarDumper Component For Debugging

What is the Symfony VarDumper Component?

The VarDumper component provides mechanisms for walking through any arbitrary PHP variable. Built on top, it provides a better dump() function that you can use instead of var_dump.

Please visit the official documentation page for more detailed information about this Symfony component.

How can I install the Symfony VarDumper Component?

The quickest and easiest way is to find the package on packigst website and then use composer to install the package to your project.

To install to your project, run the following command on either Windows or Linux in the command line.

This will add the current stable package to your project and will generate an updated autoload.php file in your vendors folder.

How do I use the Symfony VarDumper Component?

To use the VarDumper component, you must make some small changes to your AppKernel.php file, you will find this in your projects app folder.

Find the following section of code:

Now add to the code as shown in the example below:

This will now allow you to use the VarDumper Component in your project and output information to the profiler bar.

This component can be used in your project files as the method dump()  is globally available from the component, you can also use the component in the command line interface too and the output will be written to STDOUT .

How to Use the Dump Method

If for example I had an entity in my project called Order and I was wanting to view the loaded entity from the database, the easiest way of achieving this would be like so:

Resulting in the following output to the profiler bar:

Symfony Profiler Dump

This is quite a useful Symfony component and can be used in any project whether it is Symfony based or not, as long as it is a PHP project, go away and play with it, enjoy!

Using the Symfony Console Component To Improve Development & Deployment

What is the Symfony Console Component?

The Console component allows you to create command-line commands. Your console commands can be used for any recurring task, such as cronjobs, imports, or other batch jobs.

Please visit the official documentation page for more detailed information about this Symfony component.

How do I use the Symfony Console Component?

You can use the Symfony Console Component in either Windows Command Prompt (CMD) or Linux  Command Line Interface (CLI), the commands will work the same for both operating systems.

You will need to make sure PHP is in your PATH variable on Windows first.

Example Console Command

Clearing the Production Cache

If for example you wanted to clear and warmup the cache for the production environment, you would execute the following commands:

Now you should have your cache cleared and rebuilt ready to show any updates if you received no errors.

Make sure you do your database migrations before you clear your cache, as this will cause errors on the frontend of your site if you have schema changes.

Hopefully you are using database migrations for your project… you are using them right? I thought so…

Dumping Your Assetic Files

If you use the Assetic bundle in your projects, here is an example of how you would regenerate the assetic cache after making changes during development:

That’s all you need to do to update your cached files.

I will add some more examples of the Symfony Console usage to future posts.

AngularJS – Removing Objects from Arrays, The Undefined Mystery…

I have been working on an AngularJS project and have come across this issue a few times. I have a DemoController like so in my setup:

Now when I use the $scope.items  in an AngularJS ng-repeat  loop it works great, as it should BUT if I try to remove an element (object) from the $scope.items  array, it always seems to remove the element BUT add an undefined value in it’s place and this undefined value is interpreted by AngularJS in the ng-repeat  loop and outputs an empty row in the template.

This causes issues if using ng-show / ng-hide  for example as even though you have no objects in the array, it still sees it as full because it is full of [undefined, undefined, undefined, ...] .

This is causing headaches and I have seen other people with same issue but the fixes don’t seem to work well or they’re bloated and stinky.

The solution I have come up with to this, that seems to work well is below.

Please also note that some people have said this is just a pure Javascript issue, well I don’t think it is, when I tried it within AngularJS and pure Javascript, I got 2 different results with the same setup and data.

If you see the example below, simply adding the line: $scope.items.pop();  seems to solve this issue.

I am unsure why this is happening, I am pretty sure it’s an AngularJS issue though as I have tried the pure Javascript way and it worked without the need for using Array.pop(); .

Feel free to comment and let me know if I am doing something wrong and I will correct this post. I would love to solve this little problem without using this method.