Can you Call a function defined in a block from the observer in magento2?
My purpose is to assign a session variable in the observer and then later retrieve it in a phtml.
Thanking you in advance.
Observer
<?php
namespace Vendor\Module\Observer\Customer;
use \Psr\Log\LoggerInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
class Authenticated implements ObserverInterface
{
protected $_responseFactory;
protected $_url;
protected $email;
protected $_coreSession;
public function __construct(
\Magento\Framework\App\ResponseFactory $responseFactory,
\Magento\Framework\UrlInterface $url,
\Magento\Customer\Model\Session $customerSession,
\Magento\Catalog\Model\Session $catalogSession,
\Magento\Framework\Session\SessionManagerInterface $coreSession
) {
$this->_responseFactory = $responseFactory;
$this->_url = $url;
$this->_catalogSession = $catalogSession;
// $this->customerSession = $customerSession;
$this->_coreSession = $coreSession;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
//get object method
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer = $observer->getEvent()->getCustomer();
$event = $observer->getEvent();
$customer = $event->getCustomer();
$email=$customer->getEmail();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "53252",
CURLOPT_URL => "http://localhost:53252/api/Account/Login",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\n\t\"Email\":\"$email\",\n\t\"Password\":\"$email\"\n}",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
"postman-token: f2d5833b-07f0-9ecc-ca24-94dd9fc58a66"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
$blockObj= $block->getLayout()->createBlock('Vendor\Module\Block\Display');
$blockObj->getCatalogSession()->setMyName('Mageplaza');
curl_close($curl);
$json = json_decode($response, true);
//$this->customerSession->setMyValue($json);
//$this->customerSession->getMyValue();
$this->_coreSession->start();
$this->_coreSession->setMessage('The Core session');
if ($err) {
echo "cURL Error #:" . $err;
} else {
//echo $response;
if($json['Response']!='FOUND'){
$customerBeforeAuthUrl = $this->_url->getUrl('test/page/view');
$this->_responseFactory->create()->setRedirect($customerBeforeAuthUrl)->sendResponse();
}
else {
return $this;
}
}
exit;
}
//$redirectionUrl = $this->url->getUrl('[test]/[page]/[[view]');
//$this->responseFactory->create()->setRedirect($redirectionUrl)->sendResponse();
//return $this;
}
Block
<?php
namespace Vendor\Module\Block;
class Display extends \Magento\Framework\View\Element\Template
{
protected $_catalogSession;
protected $_customerSession;
protected $_checkoutSession;
public function __construct(\Magento\Framework\View\Element\Template\Context $context,\Magento\Catalog\Model\Session $catalogSession,
\Magento\Customer\Model\Session $customerSession,
\Magento\Checkout\Model\Session $checkoutSession,
array $data = [])
{
$this->_catalogSession = $catalogSession;
$this->_checkoutSession = $checkoutSession;
$this->_customerSession = $customerSession;
parent::__construct($context);
}
public function _prepareLayout()
{
return parent::_prepareLayout();
}
public function getCatalogSession()
{
return $this->_catalogSession;
}
public function getCustomerSession()
{
return $this->_customerSession;
}
public function getCheckoutSession()
{
return $this->_checkoutSession;
}
public function sayHello()
{
return __('Hello World');
}
public function getValue(){
$this->_coreSession->start();
return $this->_coreSession->getMessage();
}
}
phtml
<?php
$blockObj= $block->getLayout()->createBlock('Vendor\Module\Block\Display');
echo $blockObj->sayHello();
//$block->getCatalogSession()->setMyName('Mageplaza');
echo $block->getCatalogSession()->getMyName() . '<br />';
?>