简体   繁体   中英

Fatal Error Call to a member function login() on a non-object PHP

i am just starting with OOP in PHP, i try to make easy LOGIN form which just check in DB. But when i load the page it just throw me error which are in the name of this topic. Any idea how can i fix this?

<?php

use Nette\Application\UI;
use Nette\Database\Connection;


/**
 * Homepage presenter.
 */          
class HomepagePresenter extends BasePresenter 
{
    protected function createComponentSign()
    {
        $form = new UI\Form;
        $form->addText('name', 'Jméno:');
        $form->addPassword('password', 'Heslo:', 30);
        $form->addCheckbox('persistent', 'Pamatovat si mě na tomto počítači');
        $form->addSubmit('login', 'Přihlásit se');
        $form->onSuccess[] = callback($this, 'signSubmited');
        return $form;
    }

    // volá se po úspěšném odeslání formuláře
    public function signSubmited(UI\Form $form)
    {
        try {
            $user = $form->getValues()->name;
            $values = $form->getValues();
            if ($values ->persistent)  {
                        $user->setExpiration('+30 days',FALSE);
                        }
            $user->login($values->username, $values->password);
            $this->flashMessage("Byl jsi úspěšně přihlášen jako: $values[name]");
            //this->redirect('Homepage:');
        } catch (Nette\Security\AuthenticationException $e) {
            $form->addError('Neplatné uživatelské jméno nebo heslo.');
            }
    }   

    public function actionOut()
    {
        $this->getUser()->logout();
        $this->flashMessage('Bol si odhlasený.');
        $this->redirect('in');
    }
} 

You have the following line of code in your try/catch block

   $user = $form->getValues()->name;

which assigns $user a string value, I assume you wanted the following assignment

   $user = $this->getUser();

$user is string value and you are trying call function with that which is not correct.

$user must be object which contains login method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM