How do I instantiate and use a $session variable? I cant get through the documentation

Notice: This thread is very old.
Timon
Member | 4
+
0
-

Hi, I am really novice in php and nette though I am learning and liking it.
I wish to know how to set a session and set values. The following is a copy paste from the documentation and it is just giving errors!!

namespace App\Presenters;

use Nette;
use App\Model;
use Nette\Application\UI\Form;
use Nette\Http\SessionSection;


class HomepagePresenter extends BasePresenter
{
    private $database;
    private $session;
    private $sessionSection;

    public function __construct(Nette\Database\Context $database, Nette\Http\Session $session)
    {
        $this->database = $database;
		$session = $this->getSession(); // I dont understand this
        $mySection = $this->getSession('mySection'); //I dont understand this
    }

	// function that calls loginFormSucceeded

	public function loginFormSucceeded($form, $values)
    {
		//login
		//if login set sessions

		// variable writing
		$section->userName = 'franta'; //Why use $section yet we used $session above??!!
	}
}

It may seem very easy to most of you but I just cant get my head round it.
A sample presenter class with database and session will really help.
Please help. Thanks

CZechBoY
Member | 3608
+
+1
-

Presenter already has session. So you dont need to inject it via constructor.
In the action method you can use session.

public function actionDefault ()
{
    $this->getSession()->myVar = 'myValue';
}
Timon
Member | 4
+
0
-

Thanks alot CZechBoY!! I now understand