Empty User Object from Symfony after login -
i created login page symfony , twig. now, i'm trying retrieve username after login. seems, user object return securitycontroller empty , twig has rendering problem. need userobject?
security.yml
security: encoders: userbundle\entity\user: algorithm: bcrypt providers: in_memory: memory: ~ user_db_provider: entity: class: userbundle:user property: username firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false main: anonymous: ~ http_basic: ~ provider: user_db_provider form_login: login_path: /login check_path: /login_check logout: path: /logout target: /
securitycontroller
class securitycontroller extends controller{
/** * @route("/login", name="login_form") */ public function loginaction(){ $authenticationutils = $this->get('security.authentication_utils'); $error = $authenticationutils->getlastauthenticationerror(); $user = $this->getuser(); return $this->render(':security:login.html.twig', array( 'user' => $user, 'error'=> $error, ) ); } /** * @route("/login_check", name="login_check") */ public function logincheckaction() {} /** * @route ("/logout", name="logout") */ public function logoutaction(){}}
user (serialize, getter/setter methods implemented)
/** * @table("users") * @entity */ class user implements userinterface, \serializable { /** * @column(name="id", type="integer") * @id * @generatedvalue(strategy="auto") */ private $id; /** * @column(length=25) */ private $username; /** * @column(length=64) */ private $password; /** * @var array * @column(name="roles", type="json_array") */ private $roles = array();
twig-template
{% if is_granted('is_authenticated_remembered') %} <div class="form-group"> {{ user.username }} <a href="{{ path('logout') }}">logout</a> </div> {% else %} <form class="navbar-form navbar-left" action="{{ path('login_check') }}" method="post"> <div class="form-group"> <input type="text" class="form-control" name="_username" placeholder="username"/> <input type="password" class="form-control" name="_password" placeholder="password"/> <button type="submit" class="btn btn-default">log in</button> </div> </form> {% endif %} {% if error defined %} <p class="navbar-text navbar-right">{{ error.messagekey|trans(error.messagedata, 'security')}}</p> {% endif %}
symfony have global template variables check this. use:
{{ app.user.username }}
Comments
Post a Comment