We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Vokuro how to get data from actual logged user in view ... using volt

Hi everybody,

I am using vokuro and volt.

The login module is still the vokuro standard, but how can I set the user data into multiple views?

something like:

{{user.id}}

doesn't work.

public function loginAction()
{
        $form = new LoginForm();

        try {

            if (!$this->request->isPost()) {

                if ($this->auth->hasRememberMe()) {
                    return $this->auth->loginWithRememberMe();
                }
            } else {

                if ($form->isValid($this->request->getPost()) == false) {
                    foreach ($form->getMessages() as $message) {
                        $this->flash->error($message);
                    }
                } else {

                    $this->auth->check(array(
                        'email' => $this->request->getPost('email'),
                        'password' => $this->request->getPost('password'),
                        'remember' => $this->request->getPost('remember')
                    ));

                    return $this->response->redirect('users');
                }
            }
        } catch (AuthException $e) {
            $this->flash->error($e->getMessage());
        }

        $this->view->form = $form;
    }

Rgds

Stefan



85.5k
edited Sep '15

the idea is:

your "auth" inside the controllers is vokuro/app/library/Auth/Auth.php

now.... inside you have getUser() function

it will return instance of Vokuro\Models\Users and i think should be linked with profile model also.

so in your controller view you should be able to call

$this->auth->getUser()->id;

// i am new to phlacon so please dont curse me if it doesnt work :D

edited Sep '15

If you want user user.id in view first you have to set user in view, you can do it by two ways:

Set it using properties(i dont like it myself) like this:

$this->view->user = $youruser;

or using ->setVar('user',$youruser);

Then it will be available in your view.

If user is stored in form then just access user like {{form.user.id}}



60.0k
edited Sep '15

Hi Hristomir, hi Jurigag,

I am new to phalcon and I need an example, do you have one?

The users are not stored in an form, should I do that?

The user.id is not important to display, it is only an example. I need multiple mysql tables to display in different views e.g. region, country, user etc. in sedcard.phtml, gallery.phtml etc.

Rgds

Stefan



145.0k
Accepted
answer
edited Sep '15

Something like that should work:

$this->view->user = $this->auth->getUser();

or

$this->view->setVar('user',$this->auth->getUser();

if $this->auth not working then try $this->di->get('auth')



60.0k
edited Sep '15

Hi Jurigag,

damn i was in the totaly wrong controller :-))))))

Now it works ;-) I have lost my overview

I set it in the indexAction of the ProfileController.php for the view profile/index.volt. Is that ok?

public function indexAction(){
        $this->view->setTemplateBefore('private');
        $this->view->setVar('user',$this->auth->getUser());
}

Thx Jurigag for your time