Is there any way to access current logged in user session identity variable in file located in the public folder

Hi, Iam developing a small project in using phalcon, so my question is " Is there any way to access current logged in user session identity variable in file located in the public folder" pls help me. iam new to phalcon.



2.5k

Perhaps a broader explanation of what you're trying to do is in order here...



10.6k

I access the current user via ajax. Here is the sample code:

Javascript (this is Angular, but $.ajax will also work if you're using jquery)

    function getUser() {
        CallCenter.get("/ajax/user").then(
            function(data){
                $rootScope.$broadcast("user",data.user);
            }
        );
    }

Route:

$router->addGet("/ajax/user",[
    'controller' => 'noauthajax',
    'action' => 'authuser'
]);

Controller:

    public function authuserAction() {
        $response = $this->getResponse(true);
        $user = $this->session->get('auth');

        if (!empty($user)) {
            $user->country = $user->getCountry();
            $user->state = $user->getState();
            if ($user->role == "vendor") {
                $user->vendor = $user->getVendor();
            }
        }
        $response['data']['user'] = $user;
        echo json_encode($response);
        die();
    }
edited Jul '15

Thank you for the reply. Can you pls explain me using jQuery, since i dont know about Angular.



10.6k

Sure, it would look something like this:

    var user; //or this.user if you're in closure
    $.ajax("/ajax/user", {
        success: function(data) {
            user = data.user;
        },
        error: function() {
            //handle error
        }
   });