Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous user object in symfony

I'm using the basic user login/logout system provided with Symfony and it works fine as long as people log in. In that case the $user object is always provided as needed.

The problem is then when logged out (or not lgged in yet) there is no user object. Is there a possibility to have (in that case) a default user object provided with my own default values?

Thanks for your suggestions

like image 535
PBR Avatar asked Mar 03 '14 19:03

PBR


1 Answers

Because the solution mention above by @Chopchop (thanks anyway for your effort) didn't work here I wrote a little workaround.

I created a new class called myController which extends Controller. The only function i override is the getUser() function. There I implement it like this:

public function getUser()
{
    $user = Controller::getUser();

    if ( !is_object($user) )            
    {
        $user = new \ACME\myBundle\Entity\User();

        $user->setUserLASTNAME ('RaRa');
        $user->setID (0);
        // etc...
    }

    return $user;
}

This works fine for me now. The only problem is that you really have to be careful NOT to forget to replace Controller by myController in all your *Controller.php files. So, better suggestions still welcome.

like image 118
PBR Avatar answered Sep 28 '22 23:09

PBR