Chapters
-
Course Code
Compatible PHP versions: ^7.1.3
Compatible PHP versions: ^7.1.3
- This Video
- Subtitles
- Course Script
Debugging & Packs
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Symfony has even more debugging tools. The easiest way to get all of them is to find your terminal and run:
composer require debug --dev
Find your browser, surf back to symfony.sh and search for "debug". Ah, so the debug
alias will actually install a package called symfony/debug-pack
. So... what's a pack?
Click to look at the package details, and then go to its GitHub repository.
Whoa! It's just a single file: composer.json
! Inside, it requires six other libraries!
Sometimes, you're going to want to install several packages at once related to one feature. To make that easy, Symfony has a number of "packs", and their whole purpose is give you one easy package that actually installs several other libraries.
In this case, composer require debug
will install Monolog - a logging library, phpunit-bridge
- for testing, and even the profiler-pack
that we already installed earlier.
If you go back to the terminal... yep! It downloaded all those libraries and configured a few recipes.
And... check this out! Refresh! Hey! Our Twig dump()
got prettier! The debug-pack
integrated everything together even better.
Unpacking the Pack!
Go back to your Twig template and remove that dump. Then, open composer.json
. We just installed two packs: the debug-pack
and the profiler-pack
:
{ | |
Show Lines
|
// ... lines 2 - 15 |
"require-dev": { | |
Show Lines
|
// ... line 17 |
"symfony/debug-pack": "^1.0", | |
Show Lines
|
// ... line 19 |
"symfony/profiler-pack": "^1.0" | |
}, | |
Show Lines
|
// ... lines 22 - 65 |
} |
And we now know that the debug-pack
is actually a collection of about 6 libraries.
But, packs have a disadvantage... a "dark side". What if you wanted to control the version of just one of these libraries? Or what if you wanted most of these libraries, but you didn't want, for example, the phpunit-bridge
. Well... right now, there's no way to do that: all we have is this one debug-pack
line.
Don't worry brave space traveler! Just... unpack the pack! Yep, at your terminal, run:
composer unpack debug
The unpack
command comes from Symfony flex. And... interesting! All it says is "removing symfony/debug-pack". But if you look at your composer.json
:
{ | |
Show Lines
|
// ... lines 2 - 15 |
"require-dev": { | |
"easycorp/easy-log-handler": "^1.0.2", | |
Show Lines
|
// ... line 18 |
"symfony/debug-bundle": "^3.3|^4.0", | |
Show Lines
|
// ... line 20 |
"symfony/monolog-bundle": "^3.0", | |
"symfony/phpunit-bridge": "^3.3|^4.0", | |
"symfony/profiler-pack": "^1.0", | |
"symfony/var-dumper": "^3.3|^4.0" | |
}, | |
Show Lines
|
// ... lines 26 - 69 |
} |
Ah! It did remove symfony/debug-pack
, but it replaced it with the 6 libraries from that pack! We can now control the versions or even remove individual libraries if we don't want them.
That is the power of packs!
26 Comments
Hey Maxime,
It sounds like a CORS problem... or probably your assets are just not available. Could you open the URL that is failed with net::ERR_ABORTED error in a new tab? Did you see the same 404 error or you can load the asset successfully?
Cheers!
Hello, can you make a tutorial on how to debug xdebug when it does not stop on breakpoints. It just not stop without giving any message. In phpstorm and docker. I am sick of this. When running from command line.
Hey Lijana Z.!
Yea... this stuff is the worst. We don't have any plans to cover this, but I'll add it to our idea list - it's a question we DO get from time-to-time.
Good luck!
Hello guys,
After running composer require debug pack, i have this depreciations showing up on my browser down the page
error('A tree builder without a root node is deprecated since Symfony 4.2 and will not be supported anymore in 5.0.')
and another one
$rootNode = $treeBuilder->root('sensio_framework_extra', 'array');
I think i should replace "sensio_framework_extra " by " symfony_framework_extra " . What do you think about that.
I don't want to blow up my project so i am looking for advices.
Thank you.
Hey yao!
Ah, interesting! So, don't worry about this :). This deprecation is not caused by your code, but bu SensioFrameworkExtraBundle. So, it needs to be fixed in that bundle (and then eventually you can upgrade to the fixed version). In fact, this was fixed a few days ago in that bundle (https://github.com/sensiolabs/SensioFrameworkExtraBundle/pull/594) and is available in the latest version of the bundle. Try running composer update sensio/framework-extra-bundle
to get the latest version.
Cheers!
I have a question about how Composer works.
I want to make the composer.json file at the root of the project minimalist.
There is a line in the project root in the composer.json file:
"symfony / profiler-pack": "^ 1.0"
In "symfony / debug-pack" composer.json is:
"symfony / profiler-pack": "*"
Can I remove the line from the root composer.json:
"symfony / profiler-pack": "^ 1.0"
How to do it better? Manually or some team in the composer?
Hey Maxim M.!
Hmm... yes, I think that would work just fine :). As you mentioned, your project depends on symfony/debug-pack, and it depends on symfony/profile-pack, so you should be able to remove symfony/profiler-pack and have no changes.
To test it, run this:
composer remove symfony/profiler-pack
If we're correct, then you will ONLY see symfony/profiler-pack being removed - you will not see any other libraries being removed.
Cheers!
Yes, the "composer remove symfony/profiler-pack" command really works.
Removed "symfony/profiler-pack": "^ 1.0" from the composer.json project. As needed.
And in the /vendor directory there is symfony/profiler-pack, as required.
Thank!
Sorry in advance for not extensively investigating but executing a `{{ dump() }}` in twig (as instructed in the previous video "Web Debug Toolbar & the Profiler!") results in the `app` variable being effectively `var_dump`ed causing an out of memory error. Installing the debug recipe seemed to prevent this and also resulted in a prettier output (as shown in this video).
Hey Czar P.!
You're 100% correct. Without the debug-pack installed, dump() in twig does exactly what you said: it's a var_dump(). The reason I didn't get a memory error is because I have XDebug installed, which makes var_dump() a bit prettier, and avoids recursive memory issues. But, either way, the debug pack gives us a much better situation anyways :).
Cheers!
Sir weaverryan I'm a fan of your work in the Sf community! Appreciate the enlightenment on XDebug. I just installed it and it worked like a charm (now my output matches the previous video). Actually, I was originally just concerned with the dump behavior throwing off other learners before they get to this chapter.
Hey Czar P.!
Ahh, thank you! Made my morning :).
> Appreciate the enlightenment on XDebug. I just installed it and it worked like a charm
Awesome! Super happy that worked!
> I was originally just concerned with the dump behavior throwing off other learners before they get to this chapter
When you saw your message, i had the same thought! I wish I had thought about that when originally recording it. If we get another message about this, we'll add a note to the video.
Cheers!
Make at least a note with the text that in the previous lesson xdebug was used instead of var_dump.
This is confusing when learning, because it seems that something is not working as the author of the video and it needs to be fixed.
Hey Maxim M.!
I think that's an excellent suggestion - it confuses too many people (understandably). We'll add a note.
Cheers!
In previous chapter, I made prettier controller dump but ugly twig dump.
After I view this chapter, I made a prettier twig dump but ugly controller dump.
Who can help me to make prettier both controller dump and twig dump? My composer dev is following:
"require-dev": {
"easycorp/easy-log-handler": "^1.0.2",
"symfony/debug-bundle": "^3.3|^4.0",
"symfony/dotenv": "^4.0",
"symfony/monolog-bundle": "^3.0",
"symfony/phpunit-bridge": "^3.3|^4.0",
"symfony/profiler-pack": "^1.0",
"symfony/var-dumper": "^3.3|^4.0"
},
Hey Ramsey Jiang
What you mean with that you made controller's dump uglier?
When you dump something in a controller, don't you see a new input at your profiler's bar?
Cheers!
Hey MolloKhan
You know, at the previous chapter, I could use dump in my controller. I could view a background color is black of that dump. In the twig, I also could use dump, but the dump is as the php var_dump.
After I viewed this chapter, the dump in my controller as the php var_dump. the dump in my twig, it has a black background color.
Now do you make sence what I mean in my question?
Hey Ramsey Jiang!
Hmm. This should not be the situation. You're 100% correct that, before this chapter, the dump()
function in Twig was using var_dump
. That is Twig's default functionality. But when you install the debug-pack, Symfony replaces this with the VarDumper component implementation (that is responsible for the pretty, black background).
But in PHP, the dump
function is provided by the VarDumper component. If that component is not present, then the dump()
function should not exist. And, of course, if it IS present, the VarDumper always prints "pretty". I can't think of how the dump()
function would ever produce the uglier, var_dump version of the code :/. Just in case, try clearing your cache:
php bin/console cache:clear
If that doesn't work, could you post your controller code? I know it's simple - but I'm really not sure what the issue could be :).
Cheers!
Hey weaverryan
Your first part is 100% correct. Thanks what I have done before this chapter.
The second part, in my PHP, var_dump works, but in symfony4, dump() is not work. I don't why. That's why I ask at here hope someone can help me to fix it. I did cache clear, but the problem is still there.
You know, in my controller, dump() it's not work, it won't output anything. Only var_dump() works. but the output is uglier not prettier. Before this chapter, dump() in my controller works and pretty. My controller code is following:
/**
* @Route("/article/{slug}")
*/
public function testArticle ($slug)
{
$comments = [
'I ate a normal rock once. It did NOT taste like bacon!',
'Woohoo! I\'m going on an all-asteroid diet!',
'I like bacon too! Buy some from my site! bakinsomebacon.com',
];
dump($comments, $this);//dump at here is not work, it cannot do anything. Only var_dump works.
return $this->render('article/show.html.twig', [
'title' => $slug,
'comments' => $comments
]);
}
Hey Ramsey,
Please, make sure you have installed Var Dumper component with:
composer info symfony/var-dumper
IIRC, that this works in dev/test environments only. If you're in prod - it probably won't work. And keep in mind that in PHP files dump() works in a bit different way than in Twig templates: if you dump something in PHP file - you won't see the output on the page, but you can see it in the Symfony Web Debug Toolbar below.
P.S. If your output of var_dump() is just black/white, i.e. not colorful and nice - then you need to install and enable Xdebug PHP extension which makes var_dump() output much nicer.
Cheers!
victor In controller, I want to dump($var);die;
- How can I achieve this? Forcing us to render a html template (so that profiler will shown) is not a good solution. What if I am working on AJAX endpoint, no html? If I am not wrong, before we could use it in controller out of the box. Correct?
Well I can create a new Service with the below function:
function mydumper(array $arguments) {
dump($arguments);
return new Response('<html><body></body></html>');
}
In this case it works correctly; blank screen + debug bar + my dump inside and nothing else. But is this the right way?
Hey Yahya,
In Symfony 4 there's a "dd()" function which is equal to "dump(); die;" - you can upgrade and use it. But you can totally do like you said, i.e. manually add "die;" after "dump()": "dump($var); die;" - so your output won't go to the Web Debug Toolbar, instead, it will be printed in browser.
But if we're talking about end points that return JSON - probably showing Web Debug Toolbar is not a good idea, because your JSON response is invalid in this case. If you send AJAX requests - you can either find links to profiler pages of those requests (if you hover over AJAX icon in Web Debug Toolbar) or I use Google Chrome Debug Toolbar to open the request and see its content with dumped value, but in this case probably would be better to use var_dump() or print_r() functions instead, because dump()'s output contain extra HTML for syntax highlighting.
I hope it helps :)
Cheers!
@Victor, thank you fy message but I believe I couldn't tell it properly. It is not about dd() or dump();die; It is about viewing the dump :) Here it is: I use Symfony 4.1, debug-pack and profiler-pack, all are installed. I am creating a new controller, ie. UserController and creating a new method, ie. list.
/**
* @Route("/users/list", name="users_list", methods={"GET"})
* @param Request $request
* @param EntityManagerInterface $entityManager
*/
public function list(Request $request,
EntityManagerInterface $entityManager)
{
$repo = $entityManager->getRepository(User::class);
$records = $repo->findAll();
dd($records);
}
Boom! Blank screen. Before it was shown on the screen. Am I missing something? Check out please, I didn't decide to render a twig template, or respond in json. I just want to see the output of dump() or dd(). Blank screen!
Hey Yahya E.
If you are getting a blank screen, it means that the dd();
statement did execute, but probably your "$records" variable was empty? Try changing it to var_dump('hello', $records); die;
If that's not the case, I believe you require to install and activate "x-debug" in your system so the "dump" function works properly.
Cheers!
MolloKhan @MolloKhan Tonight, I run "composer update" that moves me from v4.1.0 => v4.1.1. Now it works as expected. So we can mark as "solved". Thank you fy help.
"Houston: no signs of life"
Start the conversation!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"sensio/framework-extra-bundle": "^5.1", // v5.1.3
"symfony/asset": "^4.0", // v4.0.3
"symfony/console": "^4.0", // v4.0.14
"symfony/flex": "^1.0", // v1.17.6
"symfony/framework-bundle": "^4.0", // v4.0.14
"symfony/lts": "^4@dev", // dev-master
"symfony/twig-bundle": "^4.0", // v4.0.3
"symfony/web-server-bundle": "^4.0", // v4.0.3
"symfony/yaml": "^4.0" // v4.0.14
},
"require-dev": {
"easycorp/easy-log-handler": "^1.0.2", // v1.0.4
"sensiolabs/security-checker": "^5.0", // v5.0.3
"symfony/debug-bundle": "^3.3|^4.0", // v4.0.3
"symfony/dotenv": "^4.0", // v4.0.14
"symfony/monolog-bundle": "^3.0", // v3.1.2
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.0.3
"symfony/profiler-pack": "^1.0", // v1.0.3
"symfony/var-dumper": "^3.3|^4.0" // v4.0.3
}
}
Hey
CSS and Images are not loading for me neither, I have a net::ERR_ABORTED 404 (Not Found).
My Symfony app is running in a docker container.
Shoud I have webpack for this?
Thanks!