0

I am setting custom registry in my module like below.

here is my controller file code.

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Session\SessionManagerInterface;

class Childcategory extends Action
{
protected $categoryRepository;
protected $resultJsonFactory;
protected $_coreSession;
protected $_coreRegistry = null;

public function __construct(
        Context $context, 
        CategoryRepositoryInterface $categoryRepository, 
        JsonFactory $jsonFactory,
        SessionManagerInterface $coreSession,
        \Magento\Framework\Registry $coreRegistry
        )
{
    $this->categoryRepository = $categoryRepository;
    $this->resultJsonFactory = $jsonFactory;
    $this->_coreSession = $coreSession;
    $this->_coreRegistry = $coreRegistry;
    parent::__construct($context);
}

public function execute()
{
    $parentId = $this->getRequest()->getParam('category_id');  
    $this->_coreRegistry->register('category_parent_id', $parentId);
    $category = $this->categoryRepository->get($parentId);
    $childrenData = [];
    return $this->resultJsonFactory->create()->setData($childrenData);
 }


  }

Block file code.

 class Category extends \Magento\Framework\View\Element\Template{
protected $categoryFactory; 
protected $_storeManager;    
protected $_categoryCollectionFactory;
protected $_coreRegistry;
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
    \Magento\Framework\Registry $coreRegistry
)
{    
    $this->categoryFactory = $categoryFactory;
    $this->_storeManager = $storeManager;
    $this->_categoryCollectionFactory = $categoryCollectionFactory;
    $this->_coreRegistry = $coreRegistry;
    parent::__construct($context);
}

public function getCatId()
{
    $sessionId = $this->_coreRegistry->registry('category_parent_id');      
    if(isset($sessionId) && $sessionId != ''){
        return $sessionId;
    }else{
        return;
    }
    }
 }

If i try to access the registry value in my template file like below.

$registryId = $block->getCatId();

I am not getting the value, but i have set it in my controller.

Where i am doing wrong, can anyone help me please.

2

you are using for controller output.

return $this->resultJsonFactory->create()->setData($childrenData);

there is no block call in this controller, so you can't get value of registry in your block. You have to render your layout or call your block directly controller.

  • you mean i have not defined block class for this controller? – jafar pinjar Jan 7 at 11:29
  • @jafarpinjar yes – Qaisar Satti Jan 7 at 11:59
0

Change the block code like this .

class Category extends \Magento\Framework\View\Element\Template
{
   protected $categoryFactory; 
   protected $_storeManager;    
   protected $_categoryCollectionFactory;
   protected $_coreRegistry;

   public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
    \Magento\Framework\Registry $coreRegistry)
{    
    $this->categoryFactory = $categoryFactory;
    $this->_storeManager = $storeManager;
    $this->_categoryCollectionFactory = $categoryCollectionFactory;
    $this->_coreRegistry = $coreRegistry;
    parent::__construct($context);
}

public function getCatId()
{
    $sessionId = $this->_coreRegistry->registry('category_parent_id');      
    if(isset($sessionId) && $sessionId != '')
    {
        return $sessionId;
    }
    else
    {
        return;
    }
 }
}
  • hi @Dhiren, where is mistake? seems the code is same – jafar pinjar Jan 4 at 14:23
  • protected $_coreRegistry; no need to set null there. – Dhiren Vasoya Jan 4 at 14:23
  • yes @Dhiren, used this code, still i am not getting value, cleared the cache also – jafar pinjar Jan 4 at 14:34
  • I can get value in controller, but from block file, its not returning value. – jafar pinjar Jan 4 at 14:40
  • Have you tried this? Is it working for you @Dhiren? – jafar pinjar Jan 4 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.