1

I need to decode this Symfony block of code that returns from my session data and then find the userid and username and email of that person. So I managed to fetch data like this but not worked for user info part :

$session =  $this->get('session');
$userSee = $session->get('_security_user.context')->getToken()->getUser();
var_dump($session); 

Result :

 object(Symfony\Component\HttpFoundation\Session\Session)#105 (3) {
  ["storage":protected]=>
  object(Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage)#107 (5) {
    ["bags":protected]=>
    array(2) {
      ["attributes"]=>
      object(Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag)#106 (4) {
        ["namespaceCharacter":"Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag":private]=>
        string(1) "/"
        ["name":"Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag":private]=>
        string(10) "attributes"
        ["storageKey":"Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag":private]=>
        string(15) "_sf2_attributes"
        ["attributes":protected]=>
        &array(1) {
          ["_security_user.context"]=>
          string(1995) "C:74:"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":1906:{a:3:{i:0;N;i:1;s:17:"username.password";i:2;s:1851:"a:4:{i:0;O:30:"AVCMS\Bundles\Users\Model\User":2:{s:7:"*data";a:19:{s:2:"id";s:1:"1";s:8:"username";s:5:"admin";s:8:"password";s:60:"$2y$09$/hLaciuGQyCLkpropsW0NOrW8uNoTSwKCPO2yDW4NnWlmhapRGnLu";s:5:"email";s:19:"test@gmail.com";s:15:"email_validated";s:1:"0";s:5:"about";s:0:"";s:9:"role_list";s:16:"ROLE_SUPER_ADMIN";s:8:"location";s:0:"";s:9:"interests";s:0:"";s:7:"website";s:0:"";s:6:"joined";s:10:"1456808076";s:6:"avatar";s:0:"";s:11:"cover_image";s:0:"";s:15:"registration_ip";N;s:7:"last_ip";s:3:"::1";s:13:"last_activity";s:10:"1456814919";s:14:"receive_emails";s:1:"1";s:4:"slug";s:5:"admin";s:19:"last_profile_update";s:1:"0";}s:14:"*subEntities";a:5:{s:8:"facebook";O:59:"AVCMS\Bundles\FacebookConnect\Model\FacebookConnectOverflow":2:{s:7:"*data";a:1:{s:2:"id";N;}s:14:"*subEntities";a:0:{}}s:8:"referral";O:46:"AVCMS\Bundles\Referrals\Model\ReferralOverflow":2:{s:7:"*data";a:1:{s:8:"referral";s:1:"0";}s:14:"*subEntities";a:0:{}}s:6:"points";O:41:"AVCMS\Bundles\Points\Model\PointsOverflow":2:{s:7:"*data";a:1:{s:6:"points";s:1:"0";}s:14:"*subEntities";a:0:{}}s:8:"messages";O:52:"AVCMS\Bundles\PrivateMessages\Model\MessagesOverflow":2:{s:7:"*data";a:1:{s:12:"total_unread";s:1:"0";}s:14:"*subEntities";a:0:{}}s:5:"group";O:35:"AVCMS\Bundles\Users\Model\UserGroup":2:{s:7:"*data";a:10:{s:2:"id";s:16:"ROLE_SUPER_ADMIN";s:4:"name";s:11:"Super Admin";s:18:"flood_control_time";s:1:"0";s:13:"admin_default";s:5:"allow";s:12:"perm_default";s:5:"allow";s:17:"moderator_default";s:5:"allow";s:16:"elevated_default";s:4:"deny";s:5:"owner";s:3:"app";s:18:"custom_permissions";s:1:"0";s:18:"admin_panel_access";s:1:"1";}s:14:"*subEntities";a:0:{}}}}i:1;b:1;i:2;a:1:{i:0;O:41:"Symfony\Component\Security\Core\Role\Role":1:{s:47:"Symfony\Component\Security\Core\Role\Rolerole";s:16:"ROLE_SUPER_ADMIN";}}i:3;a:0:{}}";}}"
        }
      }
      ["flashes"]=>
      object(Symfony\Component\HttpFoundation\Session\Flash\FlashBag)#111 (3) {
        ["name":"Symfony\Component\HttpFoundation\Session\Flash\FlashBag":private]=>
        string(7) "flashes"
        ["flashes":"Symfony\Component\HttpFoundation\Session\Flash\FlashBag":private]=>
        &array(0) {
        }
        ["storageKey":"Symfony\Component\HttpFoundation\Session\Flash\FlashBag":private]=>
        string(12) "_sf2_flashes"
      }
    }
    ["started":protected]=>
    bool(true)
    ["closed":protected]=>
    bool(false)
    ["saveHandler":protected]=>
    object(Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy)#109 (4) {
      ["handler":protected]=>
      object(SessionHandler)#110 (0) {
      }
      ["wrapper":protected]=>
      bool(true)
      ["active":protected]=>
      bool(true)
      ["saveHandlerName":protected]=>
      string(5) "files"
    }
    ["metadataBag":protected]=>
    object(Symfony\Component\HttpFoundation\Session\Storage\MetadataBag)#108 (5) {
      ["name":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBag":private]=>
      string(10) "__metadata"
      ["storageKey":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBag":private]=>
      string(9) "_sf2_meta"
      ["meta":protected]=>
      &array(3) {
        ["u"]=>
        int(1456814934)
        ["c"]=>
        int(1456814130)
        ["l"]=>
        string(1) "0"
      }
      ["lastUsed":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBag":private]=>
      int(1456814931)
      ["updateThreshold":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBag":private]=>
      int(0)
    }
  }
  ["flashName":"Symfony\Component\HttpFoundation\Session\Session":private]=>
  string(7) "flashes"
  ["attributeName":"Symfony\Component\HttpFoundation\Session\Session":private]=>
  string(10) "attributes"
}

Error when I return $userSee

<b>Fatal error</b>:  Call to a member function getToken() on string in <b>\GameModulesController.php</b> on line <b>157</b><br />
Mac Taylor
  • 5,020
  • 14
  • 50
  • 73

2 Answers2

3

If your controller extends Symfony\Bundle\FrameworkBundle\Controller\Controller, you can call method getUser().

For example:

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

/**
 * Default controller
 *
 * @Route("/app")
 */
class DefaultController extends Controller
{
    /**
     * @Route("/", name="app_index")
     */
    public function indexAction()
    {
        dump($this->getUser());
        return $this->render('default/index.html.twig');
    }
}
jkucharovic
  • 4,214
  • 1
  • 31
  • 46
1

For retrieve user informations in symfony2 you have 2 way depends on your version of symfony :

  • $user = $this->get('security.context')->getToken()->getUser(); WARNING : this is the deprecated method don't use this with symfony 2.6 and higher
  • $user = $this->get('security.token_storage')->getToken()->getUser(); : this is the main method official of symfony and not deprecated
miltone
  • 4,416
  • 11
  • 42
  • 76