silexphp / Silex

[DEPRECATED -- Use Symfony instead] The PHP micro-framework based on the Symfony Components
https://silex.symfony.com
MIT License
3.58k stars 718 forks source link

Silex 2 and Symfony 2.7+ (with Twig 1.18.0)'s app.user #1152

Closed J7mbo closed 9 years ago

J7mbo commented 9 years ago

Trying to use {{ app.user }} with the above versions says that it doesn't exist in the application. Is this because twig-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 some E_DEPRECATED notices. Again, switching to {{ app.user }} gives me TWIG_ERROR_RUNTIME saying that Method "user" for object does not exist.

What am I doing wrong?

J7mbo commented 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?

J7mbo commented 9 years ago

This was 5 days ago - am I doing something retarded?

GromNaN commented 9 years ago

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.

J7mbo commented 9 years ago

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.

GromNaN commented 9 years ago

It's {{ app.token_storage.token.user }}.

_Edited to use app.token_storage instead of security.token_storage that doesn't exist._

J7mbo commented 9 years ago

Thanks @GromNaN... What is the security var an instance of?

GromNaN commented 9 years ago

It's a Symfony\Component\Security\Core\SecurityContext. https://github.com/silexphp/Silex/blob/master/src/Silex/Provider/SecurityServiceProvider.php#L95-L97

stof commented 9 years ago

use app.token_storage to access the TokenStorage (given that app is your Silex Application)

stof commented 9 years ago

@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

GromNaN commented 9 years ago

Damn it. I didn't write what I had in my head. I fixed the comment.

J7mbo commented 9 years ago

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.

stof commented 9 years ago

@J7mbo sorry, the service is named security.token_storage, not token_storage. So the right code would be app.['security.token_storage'].token.user

J7mbo commented 9 years ago

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.

fabpot commented 9 years ago

See #1165 for a possible solution.