Chapters
33 Chapters
|
3:34:13
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
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!
20.
Fetching the User In a Service
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.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.2
"doctrine/doctrine-bundle": "^1.6.10", // 1.10.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.0
"knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
"knplabs/knp-time-bundle": "^1.8", // 1.8.0
"nexylan/slack-bundle": "^2.0,<2.2.0", // v2.0.0
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.0
"sensio/framework-extra-bundle": "^5.1", // v5.2.0
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.1.4
"symfony/cache": "^3.3|^4.0", // v4.1.4
"symfony/console": "^4.0", // v4.1.4
"symfony/flex": "^1.0", // v1.21.6
"symfony/framework-bundle": "^4.0", // v4.1.4
"symfony/lts": "^4@dev", // dev-master
"symfony/property-access": "^3.3|^4.0", // v4.1.4
"symfony/property-info": "^3.3|^4.0", // v4.1.4
"symfony/security-bundle": "^4.0", // v4.1.4
"symfony/serializer": "^3.3|^4.0", // v4.1.4
"symfony/twig-bundle": "^4.0", // v4.1.4
"symfony/web-server-bundle": "^4.0", // v4.1.4
"symfony/yaml": "^4.0", // v4.1.4
"twig/extensions": "^1.5" // v1.5.2
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.0.2
"easycorp/easy-log-handler": "^1.0.2", // v1.0.7
"fzaninotto/faker": "^1.7", // v1.8.0
"symfony/debug-bundle": "^3.3|^4.0", // v4.1.4
"symfony/dotenv": "^4.0", // v4.1.4
"symfony/maker-bundle": "^1.0", // v1.7.0
"symfony/monolog-bundle": "^3.0", // v3.3.0
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.1.4
"symfony/stopwatch": "^3.3|^4.0", // v4.1.4
"symfony/var-dumper": "^3.3|^4.0", // v4.1.4
"symfony/web-profiler-bundle": "^3.3|^4.0" // v4.1.4
}
}
16 Comments
care to teach us how to create a form to update the user profile ?
password field is a must, therefore we cant create a form to update profile without password :(
Hey Ad F.
That's a good question, what you can do is create a form type specific for updating a user, in that form type you can add a "UserPassword" field type, that field type has all the logic you are looking for. You can find more detailed info about it in the docs: https://symfony.com/doc/cur...
Cheers!
Thx Ryan,
But how to test service who inject Security dependence ?
ty
Hey gponty
If you are doing an unit test, then you can just mock up that dependency but if you're doing a functional test, then you can just fetch the service from the Container and use it as normal
Cheers!
I have a method in a service which creates a `Cat` entity:
```php
private function addCat(array $catData)
{
$cat = new Cat();
$cat->setName($catData['name']);
$cat->setUser($this->security->getUser());
// etc.
}
```
But I get the error that `setUser` expects a `User` entity but is getting a `UserInterface` instead. Where am I going wrong?
Hey @muureke!
Hmm, what does the setUser() method look like. Specially, what type-hint does it have (I think it’s User) and what use statement do you have for User in the Cat class?
Cheers!
Thanks for the quick response! Set user looks like this:
`
public function setUser(?User $user): self
{
}
`
...and there is no use statement for User in the Cat class. I haven't modified anything after the models and relationships were created for me by make:entity.
Hey muureke!
Sorry for the slow reply! Hmm, yea - that looks fine. Something weird is definitely happening. In that addCat() method, I would dd($this->security->getUser()) and week what you get. I’m thinking that , somehow, the authenticated User object is *not* an instance of your User entity. I’m not sure how that would happen yet, but that’s the next place to look :).
Cheers!
Hi Ryan,
Thanks for responding again! This is the result of my `dd`:
User^ {#1033 ▼
-id: 1
-email: "test@user.com"
-roles: []
-password: "$argon2i$v=19$m=65536,t=4,p=1$n8S2a5yloMxednTCdVNDOQ$j4a5T4PwRuc7dbE0Ayv55N906bF2trO2zjhM9AAojF4"
-cats: PersistentCollection^ {#1250 ▶}
}
So it's a user. And, er, here's where I have to confess: the code actually works. I've realised now that it's just PhpStorm giving me that error ("Expected parameter of type `\App\Entity\User`, `Symfony\Component\Security\Core\User\UserInterface` provided").
So: sorry for wasting your time. Although if you know why PhpStorm is giving this error it'd be nice to squish it.
Yo @muureke!
Ah :D. No worries - I get curious about this stuff too. And I can answer this :). If you looked at the PHPDoc on the
$this->security->getUser()method (https://github.com/symfony/symfony/blob/08a218c79fdc4018b67cf9ba19e601d065ea0f04/src/Symfony/Component/Security/Core/Security.php#L38) you'll see that it returns UserInterface. The reason is that this is a core Symfony function... and the only rule for a "User" object is that it is any object that implements UserInterface. Of course in YOUR application, you know thatgetUser()will return your User object, but Symfony can only know its an instance ofUserInterface.When PhpStorm see this, it says "Hey! This method returns a UserInterface... but this other method that you're passing that value to requires a User. What's going on?". Obviously, you can ignore it... but that's the reason why ;). If you were hardcore about it, you could write code like this:
That would technically be "more" correct... but I feel it's a bit of a waste of time - the exception would NEVER be hit in your app.
Cheers!
Ryan, your persistence, dedication, and erudition are magnificent and generous. Thank you!
❤️
But how does it log all information about a User?
In markdown.log there's only this information:
[2018-10-19 13:49:37] markdown.INFO: They are talking about bacon again! {"user":"[object] (App\\Entity\\User: {})"} []
There's no specific id of the user.
Doesn't it mean that it only shows the User object of a user, who is currently logged in?
Hey boykodev
> Doesn't it mean that it only shows the User object of a user, who is currently logged in?
Well, yes, the `Security::getUser()` method returns the logged in user object or null in case you are anonymous
Were you logged in? You could dump what `Security::getUser()` returns and see what it's actually returning.
Cheers!
The "Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait::getUser()" method is considered final. It may change without further notice as of its next major version. You should not extend it from "App\Controller\BaseController".
What about that deprecation message, should we ignore it? Is there another way to get user autocompletion from PhpStorm?
I know you already know this :) but just in case someone else reads it. Check the TIP on this episode: https://symfonycasts.com/sc...
"Houston: no signs of life"
Start the conversation!