Magento 2'de müşteri oturumu verileri nasıl ayarlanır ve alınır


12

Magento 2 seansı ile mücadele ediyorum. Denetleyici dosyasını aşağıda bir örnek kod olarak oluşturdum.

<?php
namespace vendor_name\module_name\Controller\SetGetSession;

use Magento\Framework\App\Action\Action;

class SetGetSession extends Action
{
    protected $customerSession;

    public function _construct(
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->customerSession = $customerSession;
    }   

    public function execute()
    {

    }
}

Herkes nasıl veri atama ve oturum değişkeninden almak için bana yardımcı olabilir?

Teşekkür ederim.

Yanıtlar:


19

Kullanarak Müşteri oturumunu ayarlayabilir ve alabilirsiniz Magento\Customer\Model\Session

protected $customerSession;

public function __construct(   
    \Magento\Customer\Model\Session $customerSession
){
    $this->customerSession = $customerSession;
}

$this->customerSession->setMyValue('test');
$this->customerSession->getMyValue();

Veya nesne yöneticisi tarafından.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
$customerSession->setMyValue('test');
$customerSession->getMyValue();
  1. Müşteri oturumuna bilgi ayarlama:
$om = \Magento\Framework\App\ObjectManager::getInstance(); $session =
$om->get('Magento\Customer\Model\Session');  
$session->setTestKey('test value');
  1. Müşteri oturumundan bilgi alma:
$om = \Magento\Framework\App\ObjectManager::getInstance();  $session =
$om->get('Magento\Customer\Model\Session');
echo $session->getTestKey();

Oturum Magento\Framework\Session\SessionManager, oturumu işlemek için çekirdek sınıfı genişletir .

Umarım bu cevap size yardımcı olacaktır.


Sağlanan set ve oturum kodu almak ile "boş bir üye işlevi setMyValue () çağrısı" olarak hata alıyorum.
— Aniket Shinde

Lütfen nesne yöneticisi tarafından eklenen değiştirilmiş cevabı kontrol edin.
— Krishna ijjada

Yardım için teşekkürler. Nesne yöneticisi ile çalışır, ancak sayfa yükleme süresini artırıyor gibi görünüyor. Soruyu göndermeden önce denedim.
— Aniket Shinde

3

\Magento\Customer\Model\SessionMüşteri oturumunda ayarlamak ve veri almak için sınıf enjekte etmeniz gerekir

Bağımlılık Enjeksiyonunu Kullanma

protected $customerSession;

public function _construct(
    ...
    \Magento\Customer\Model\Session $customerSession
    ...
) {
    ...
    $this->customerSession = $customerSession;
    ...
}   

public function setValue()
{
    return $this->customerSession->setMyValue('YourValue'); //set value in customer session
}

public function getValue()
{
    return $this->customerSession->getMyValue(); //Get value from customer session
}

Nesne Yöneticisini Kullanma

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$customerSession = $objectManager->get('Magento\Customer\Model\Session');

$customerSession->setMyValue('YourValue'); //set value in customer session
echo $customerSession->getMyValue(); //Get value from customer session
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.