6

I am looking for the right flow of data of how Magento wants me to do it. I have some data which I want to pass to the view. How do I do this the right way?

I saw the following answer for Magento 1.x https://stackoverflow.com/a/4006908/2190322

Does this apply in the same way for Magento 2? Are there differences in the flow/implementation or is it the same? If there are some differences which are these and how do I implement them?

I also saw this question: https://magento.stackexchange.com/a/82376/5827 But it has no answer regarding to best practices, which I am looking for.

1
2

You should not passing data from Controller Action to View. Use block to for passing data to View (template engine).

8
  • 2
    I am not asking to pass data from controller to a view. I chose my formulation with care. I am asking how to "get" data from a controller to a block/view. Actually you gave the same answer to the question of the second link, so I knew this allready. I am looking for a more extensive answer how to acomplish this. Maybee you can edit your answer and explain how to do it instead of what to do. Thanks.
    – Akif
    Feb 2 '16 at 14:19
  • Maybe I do not understand what it means to "get." Do you want to receive the data in block from the controller? Or Do you want to the controller set data in block?
    – KAndy
    Feb 2 '16 at 15:11
  • What I want is show the contents of a variabele which I have in the controller on thee screen of a user :) I am looking for the right flow reagarding magento conventions.
    – Akif
    Feb 2 '16 at 16:20
  • Can you move code that get data form controller to block?
    – KAndy
    Feb 2 '16 at 21:23
  • 1
    You can use \Magento\Ui\Component\MassAction\Filter in you block, i do not see reason to not do it
    – KAndy
    Feb 3 '16 at 10:51
14

One approach is to use the registry so in your controller class you put it in the registry, and then in your block you can retrieve it.

<?php

namespace <Vendor>\<ModuleName>\Controller\Index;

use Magento\Framework\App\Action\Action;

class Index extends Action
{
    protected $_coreRegistry;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Framework\View\Result\PageFactory $pageFactory
    ) {
        $this->_coreRegistry = $coreRegistry;
        $this->_resultPageFactory = $pageFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $this->_coreRegistry->register('foo', 'bar');
        return $this->_resultPageFactory->create();
    }
}

Then in your block you can do;

<?php

namespace <Vendor>\<ModuleName>\Block;

use Magento\Framework\View\Element\Template;

class Moo extends Template
{
    protected $_coreRegistry;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Registry $coreRegistry,
        array $data = []
    ) {
        $this->_coreRegistry = $coreRegistry;
        parent::__construct($context, $data);
    }

    public function getFoo()
    {
        // will return 'bar'
        return $this->_coreRegistry->registry('foo');
    }
}

Obviously you need to get your block on the page in the first place but this should give you a good start.

11
  • Thanks. Is this the best practice and/or convention?
    – Akif
    Feb 2 '16 at 19:41
  • 1
    If you take the time to look through the Magento code you will see they use this technique to store the current category and current product so it can be accessed later on.
    – Smartie
    Feb 2 '16 at 19:43
  • 3
    it's magento 1 practice, in m2 try to avoid it.
    – KAndy
    Feb 2 '16 at 21:22
  • What is your solution @KAndy ?
    – Smartie
    Feb 2 '16 at 22:32
  • @Smartie, Gets data direct in block
    – KAndy
    Feb 3 '16 at 10:57
1

You can do this way:

In your controller:

//\Magento\Framework\View\Result\PageFactory $pageFactory
$resultPage = $this->resultPageFactory->create();
// your.block.name is from your layout
$block = $resultPage->getLayout()->getBlock('your.block.name');
$block->setData('my_key', $data);

and now in template:

$data = $block->getData("my_key");

Hope this helps someone.

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.