Closed J7mbo closed 9 years ago
Addition: According to the docs, it seems there should be this GlobalVariables
class. I could create this myself, and I can only find reference to this within FrameworkBundle. Something missing in Silex / Twig?
This was 5 days ago - am I doing something retarded?
The documentation is related to Symfony fullstack, I agree that it's not obvious.
The app
variable that is accessible in Twig refers to the Silex Application. So you have access to all the methods of this class and all the services registered. And by default there is no service or method named user
.
Using the SecurityTrait
you can declare a method $app->user()
that returns the current user.
As Silex previously had app.security.token.user
available and it's deprecating accessing app.security
, which is an instance of the deprecated SecurityContext
, I'm presuming there needs to be a replacement using the new authorisation checker or token storage mechanisms. Otherwise, how is everyone using Silex going to access the user object, write their own or use the trait?
On 23 Apr 2015, at 23:27, Jérôme Tamarelle notifications@github.com wrote:
The documentation is related to Symfony fullstack, I agree that it's not obvious.
The app variable that is accessible in Twig refers to the Silex Application. So you have access to all the methods of this class and all the services registered. And by default there is no service or method named user.
Using the SecurityTrait you can declare a method $app->user() that returns the current user.
— Reply to this email directly or view it on GitHub.
It's {{ app.token_storage.token.user }}
.
_Edited to use app.token_storage
instead of security.token_storage
that doesn't exist._
Thanks @GromNaN... What is the security var an instance of?
It's a Symfony\Component\Security\Core\SecurityContext
.
https://github.com/silexphp/Silex/blob/master/src/Silex/Provider/SecurityServiceProvider.php#L95-L97
use app.token_storage
to access the TokenStorage (given that app
is your Silex Application)
@GromNaN your suggestion does not work. the SecurityContext does not have a getter for the TokenStorage, and it is also precisely the class which should be avoided as it is deprecated
Damn it. I didn't write what I had in my head. I fixed the comment.
Method "tokenstorage" for object "Silex\Application" does not exist. (slightly edited because I am in fact extending Silex\Application_)
Btw, I have registered everything as expected and all my security works fine. I also have access to the TokenStorage
and AuthorizationChecker
objects from my controllers onwards, so it definitely exists.
@J7mbo sorry, the service is named security.token_storage
, not token_storage
. So the right code would be app.['security.token_storage'].token.user
Can we split this out to app.token_storage
and app.authorization_checker
?
Using app['security.token_storage']
seems unclear to me that we're _not_ using security.token_storage
, security being an instance of the deprecated SecurityContext
.
See #1165 for a possible solution.
Trying to use
{{ app.user }}
with the above versions says that it doesn't exist in the application. Is this becausetwig-bridge
doesn't yet support it?I have registered everything as expected, so I can still use
{{ app.security.token.user }}
but then I get someE_DEPRECATED
notices. Again, switching to{{ app.user }}
gives meTWIG_ERROR_RUNTIME
saying that Method "user" for object does not exist.What am I doing wrong?