Ako na pracu s a v $session?Ak mozte,pomozte prosim
- 01laky
- Člen | 18
Ahojte.Pojdem hned na vec.Presiel som uz dokumentaciu a aj prispevky vo fore,ale zas sa neviem hnut.Ako na session?Moj BasePresenter :
<?php
namespace App\CustomerModule\Presenters;
use Nette,
App\Model;
use Nette\Application\UI\Form as Form;
/**
* Base presenter for all application presenters.
*/
abstract class BasePresenter extends Nette\Application\UI\Presenter
{
/**
* @var \App\Model\ItemsManager
* @inject
*/
public $itemsManager;
/**
* @var \App\Model\SystemManager
* @inject
*/
public $systemManager;
/**
* @var \App\Model\UserManager
* @inject
*/
public $userManager;
/** @var Nette\Http\Session */
private $session;
/** \Nette\Http\SessionSection */
private $sessionSection;
public function __construct(\Nette\Http\Session $session)
{
$this->session = $session;
$this->sessionSection = $session->getSection('basket');
}
protected function startup()
{
parent::startup();
$this->setLayout('customer');
$user = $this->getUser();
if (!$user->isInRole('customer'))
{
$user->logout(TRUE);
$this->redirect(':Public:Sign:in');
}
}
protected function beforeRender()
{
parent::beforeRender();
$this->template->items = $this->itemsManager->getItems();
$this->template->news = $this->itemsManager->news();
$this->template->solds = $this->itemsManager->bestSold();
$this->template->prices = $this->itemsManager->bestPrice();
$this->template->theme = $this->systemManager->getDefaultTheme();
$this->template->defaults = $this->systemManager->getDefaults();
$this->template->user = $this->getUser();
}
}
Ak som to spravne pochopil,mal by som ju mat nazvanu basket,ako s nou
pracovat?
Skusal som toto :
protected function createComponentBuyItem()
{
$id = $this->getParameter('id');
$form = new Form;
$item = $this->itemsManager->getItem($id);
$number = array();
for($x=1 ; $x <= $item->quantity;$x++)
{
$number[$x] = $x;
}
$form->addSelect('quantity', 'Pocet kusu ', $number)
->setAttribute('class' , 'form_select');
$form->addSubmit('send', 'Zakup')
->setAttribute('class' , 'form_button');
$form->onSuccess[] = callback($this, 'buyItemSubmitted');
$form->onSuccess[] = $this->buyItemSubmitted;
return $form;
}
public function buyItemSubmitted($form) {
$values = $form->getValues();
$id = $this->getParameter('id');
$basket->quantity = $values->quantity;
//$this->itemsManager->buyItem($values->quantity , $id);
$this->flashMessage('Zbozi zakoupeno');
$this->redirect(':Customer:Homepage:default');
}
No vyhodilo mi „Creating default object from empty value“.Navedie ma niekto prosim,ked tak aj s prihliadnutim na zaciatocnika…Dopredu dakujem
- Mysteria
- Člen | 797
Tady to máš dobře vysvětleno: https://doc.nette.org/…ttp/sessions#…
Ve zkratce, ten konstruktor z BasePresenteru vyhoď a do jeho startup
metody dej
$this->sessionSection = $this->getSession('basket');
No a pak s tím pracuješ už jednoduše:
$this->sessionSection->myVariable = 'whatever';
- 01laky
- Člen | 18
Najprv som to mal v startup metode aj ja :
<?php
namespace App\CustomerModule\Presenters;
use Nette,
App\Model;
use Nette\Application\UI\Form as Form;
/**
* Base presenter for all application presenters.
*/
abstract class BasePresenter extends Nette\Application\UI\Presenter
{
/**
* @var \App\Model\ItemsManager
* @inject
*/
public $itemsManager;
/**
* @var \App\Model\SystemManager
* @inject
*/
public $systemManager;
/**
* @var \App\Model\UserManager
* @inject
*/
public $userManager;
/** @var Nette\Http\Session */
private $session;
protected function startup()
{
parent::startup();
$this->sessionSection = $this->getSession('basket');
$this->setLayout('customer');
$user = $this->getUser();
if (!$user->isInRole('customer'))
{
$user->logout(TRUE);
$this->redirect(':Public:Sign:in');
}
}
protected function beforeRender()
{
parent::beforeRender();
$this->template->news = $this->itemsManager->news();
$this->template->solds = $this->itemsManager->bestSold();
$this->template->prices = $this->itemsManager->bestPrice();
$this->template->theme = $this->systemManager->getDefaultTheme();
$this->template->defaults = $this->systemManager->getDefaults();
}
}
Lenze mi vypisuje : Cannot write to an undeclared property App\CustomerModule\Presenters\HomepagePresenter::$sessionSection.V klasickom PHP som s nou vedel pracovat jedna radost,tu mi to vobec nejde :(
- 01laky
- Člen | 18
Jasne,dakujem za rady/Rozdiel medzi private,public a protected jasne,ze poznam…Mno problem je v tom,ze ked uz cosi neviem vyriesit par hodin,tak sa snazim dosiehnut veci akymkolvek sposobom.Cele som to mal domotane tam,v konecnom dosledku ten basket robim za pomoci databazy,na session by tam bolo az vela veci na ulozenie,Celkovo ale vdaka