Chapters
26 Chapters
|
2:53:08
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
16.
The Critical kernel.exception Event Listeners
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial also works well for Symfony 6!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2",
"ext-iconv": "*",
"antishov/doctrine-extensions-bundle": "^1.4", // v1.4.3
"aws/aws-sdk-php": "^3.87", // 3.133.20
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.12.1
"doctrine/doctrine-bundle": "^2.0", // 2.2.3
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // 2.2.2
"doctrine/orm": "^2.5.11", // 2.8.2
"easycorp/easy-log-handler": "^1.0", // v1.0.9
"http-interop/http-factory-guzzle": "^1.0", // 1.0.0
"knplabs/knp-markdown-bundle": "^1.7", // 1.9.0
"knplabs/knp-paginator-bundle": "^5.0", // v5.4.2
"knplabs/knp-snappy-bundle": "^1.6", // v1.7.1
"knplabs/knp-time-bundle": "^1.8", // v1.16.0
"league/flysystem-aws-s3-v3": "^1.0", // 1.0.24
"league/flysystem-cached-adapter": "^1.0", // 1.0.9
"league/html-to-markdown": "^4.8", // 4.9.1
"liip/imagine-bundle": "^2.1", // 2.5.0
"oneup/flysystem-bundle": "^3.0", // 3.7.0
"php-http/guzzle6-adapter": "^2.0", // v2.0.2
"phpdocumentor/reflection-docblock": "^5.2", // 5.2.2
"sensio/framework-extra-bundle": "^5.1", // v5.6.1
"symfony/asset": "5.0.*", // v5.0.11
"symfony/console": "5.0.*", // v5.0.11
"symfony/dotenv": "5.0.*", // v5.0.11
"symfony/flex": "^1.9", // v1.21.6
"symfony/form": "5.0.*", // v5.0.11
"symfony/framework-bundle": "5.0.*", // v5.0.11
"symfony/mailer": "5.0.*", // v5.0.11
"symfony/messenger": "5.0.*", // v5.0.11
"symfony/monolog-bundle": "^3.5", // v3.6.0
"symfony/property-access": "5.0.*|| 5.1.*", // v5.1.11
"symfony/property-info": "5.0.*|| 5.1.*", // v5.1.10
"symfony/routing": "5.1.*", // v5.1.11
"symfony/security-bundle": "5.0.*", // v5.0.11
"symfony/sendgrid-mailer": "5.0.*", // v5.0.11
"symfony/serializer": "5.0.*|| 5.1.*", // v5.1.10
"symfony/twig-bundle": "5.0.*", // v5.0.11
"symfony/validator": "5.0.*", // v5.0.11
"symfony/webpack-encore-bundle": "^1.4", // v1.11.1
"symfony/yaml": "5.0.*", // v5.0.11
"twig/cssinliner-extra": "^2.12", // v2.14.3
"twig/extensions": "^1.5", // v1.5.4
"twig/extra-bundle": "^2.12|^3.0", // v3.3.0
"twig/inky-extra": "^2.12", // v2.14.3
"twig/twig": "^2.12|^3.0" // v2.14.4
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.4.0
"fakerphp/faker": "^1.13", // v1.13.0
"symfony/browser-kit": "5.0.*", // v5.0.11
"symfony/debug-bundle": "5.0.*", // v5.0.11
"symfony/maker-bundle": "^1.0", // v1.29.1
"symfony/phpunit-bridge": "5.0.*", // v5.0.11
"symfony/stopwatch": "^5.1", // v5.1.11
"symfony/var-dumper": "5.0.*", // v5.0.11
"symfony/web-profiler-bundle": "^5.0" // v5.0.11
}
}
12 Comments
Can I get an explanation what exactly the "onControllerArguments" method does? There some familiar bits but I failed to get a whole picture of what it's trying to achieve.
Hey Volodymyr T.!
Ha! Good eye! I purposely didn't talk about it because it's not that important and it IS a bit confusing/complex. The flow is this:
A) Somewhere in the main request, an exception is thrown
B) This causes
ErrorListener::onKernelException()to be calledC) Inside this method (via the private
duplicateRequest()method) a new Request object is created and the exception is added as anexceptionrequest attribute.D) The Request created above is handled via a sub-request. This starts the whole request-response flow process again. The job of this sub-request is to render the error page (via the error controller)
E) Before the error controller is executed, the
onControllerArguments()method is called (this was also called on the main request before the controller, but it didn't do anything then.F)
onControllerArguments()first finds which position the$exceptionargument is in in the error controller (that's the$kvariable). Because remember, at this point, the controller arguments HAVE been determined, and since there is aexceptionkey in the request attribute, if there is an$exceptionargument in the controller, then those have been matched up.G) The method THEN looks to see what the type-hint is on the
$exceptionargument. If there is NONE or if it is type-hinted with FlattenException, then the method creates a FlattenException from the exception and uses that as the argument.So that's a LONG way of saying that this code allows your error controller to have an
$exceptionargument and choose between the real exception object and a FlattenException object via the type-hint. How crazy is that? I'm not sure the exact history behind this - it comes from the introduction of the ErrorRenderer component - but apparently there are reasons to sometimes want one class or the other.Cheers!
Thanks for the detailed explanation. It now totally became clear! I stumbled to grasp that there is a possibility for a custom error controller which might have something else in its list of arguments than the
public function __invoke(\Throwable $exception): Responseof a core "ErrorController::__invoke()" method.Hello! Thanks a lot for this course.
Could you please explain why $this->conttroller equals "error_controller"? Who provide this string?
Hey Daniil G.!
Excellent question! On a high level, the
ErrorListenerclass is a service that is registered by Symfony. It's first argument to__constructis$controller. So, on a high level, when Symfony registersErrorListeneras a service, it sets the stringerror_controlleras its first argument. You can see it right here: https://github.com/symfony/symfony/blob/75e71e3bbefffe1e67d80e842d66721b98f6a531/src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml#L81In reality, you'll notice that the first argument is a parameter -
%kernel.error_controller%. This touches on the type of stuff that we'll talk about in our next deep dive tutorial, which will be all about the Symfony container. But basically, you can set the error controller string via this config:This is not usually something that you set in your app, and it defaults to
error_controller. Whatever value is set for this (your custom value or the defaulterror_controller) is eventually set to that parameter - https://github.com/symfony/symfony/blob/75e71e3bbefffe1e67d80e842d66721b98f6a531/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php#L238 - which is passed as the first argument toErrorListener, which sets it on the$this->controllerproperty. How fun is that ;).That's a quick and dirty explanation - let me know if it helps!
Cheers!
Thanks! Good explanation.
Hi, was it the weekend?
In fact I was able to solve my problem, I explain!
In the BaseFixture class, there is the getRandomReference method. Just before the last line, I replaced this `$randomReferenceKey = $this->faker->randomElement($this->referencesIndex[$groupName]);`, by this `$randomReferenceKey = $this->faker->unique()->randomElement($this->referencesIndex[$groupName]); `
Hey Diarill
Your solution looks good, but does your issue related to course? or it was question about your own implementation? And does it related to this chapter? It's important because when you say that something not work as expected we should know is it our course code broken? or it's just your theoretical question.
Cheers!
Hi Vladimir Sadicov
no! my problem has nothing to do with the course or this video. But by following this project (The_Spacebar) from the start, I imagined that I could find myself in the situation where (for example in the case of online application submission for a position, we would not want a candidate to submit multiple times) a unique post for each account.
Hey Diarill
Sorry for late reply. Thanks for approve that course is ok. And as I said you solution is pretty good for it, probably I would recommend you to refactor it in for example
getUniqueReference()and so on. It's really depends on your needs!Cheers! and Enjoy the courses!
Hello,
When I imagine a simple senariot, I run into a problem. I assumed that a user should only publish one article, so I added the unique user_id constraint in the Article entity. But when I launch the fixtures, boom !!! "Integrity constraint violation: 1062 Duplicate entry ". What signature should I add to solve the problem.
Thanks in advance
Hey Diarill!
> When I imagine a simple senariot, I run into a problem. I assumed that a user should only publish one article, so I added the unique user_id constraint in the Article entity. But when I launch the fixtures, boom !!! "Integrity constraint violation: 1062 Duplicate entry ".
Hmm. If you only want a user to publish one article... but executing your fixtures creates this error, then it sounds like your fixtures are written incorrectly - they are assigning more than one article to a user. Is it possible that your comment is being displayed below the wrong article? This question seems unrelated... it actually kind of seems related to using Alice YAML fixtures... which is why I might be confused :). Let me know!
Cheers!
"Houston: no signs of life"
Start the conversation!