2

Do I want to know do we passe value from controller to my view?

I have my controller with the execute() function and I have a custom function in my controller (called getOrderInfos() with getting data from an XML).

getOrdersInfos() returns an array and I want this array to be displayed in my view. Do I need to use a block?

I'm new to Magento, and I'm stuck here...

Thanks

EDIT :

public function execute()
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $customerSession = $objectManager->get('Magento\Customer\Model\Session');
    if(!$customerSession->isLoggedIn()) {
        $this->_redirect('/');
    }

    // echo "<pre>";
    // print_r($this->getCustomer());
    // echo "</pre>";

    $codeClient = $customerSession->getCustomer()->getCodeClient();
    $firstName  = $customerSession->getCustomer()->getFirstname();
    $lastname   = $customerSession->getCustomer()->getLastname();

    $fullName = strtolower($lastname.'_'.$firstName) ;
    $orderInfos = $this->getClientOrder($codeClient, $fullName);

    $resultPage = $this->_resultPageFactory->create();
    return $resultPage;
}

And (same file)

public function getClientOrder($code, $name)
{
    $objectManager = ObjectManager::getInstance();
    $directory = $objectManager->get('\Magento\Framework\Filesystem\DirectoryList');
    $base_path  =  $directory->getRoot();
    $fichier = 'fichier.xml';
    $xml = simplexml_load_file($fichier);
    $codeXML = $xml->name;
    return $codeXML;
}
  • can you please add code for better idea? – Abhishek Panchal May 30 '18 at 14:39
4

You need to make a block.

Example :

Namespace/Module/Controller/Index/Index.php

    <?php

namespace Namespace\Module\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * Index constructor.
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    )
    {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        return $this->resultPageFactory->create();

    }
}

Namespace_Module/view/frontend/layout/YourRouteId_index_index.xml

layout handle you must specify your .

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Namespace_Module\Block\Example" name="delete_account_customer_page" template="Namespace_Module::your_template.phtml"/>
        </referenceContainer>
    </body>
</page>

Namespace_Module/view/frontend/templates/your_template.phtml

$block->getOrderInfos();

Namespace_Module/Block/Example.php

<?php

namespace Namespace\Module\Block;

class Example extends \Magento\Framework\View\Element\Template
{
   //here you write your method getOrderInfos()
   // get it on your template like this $block->getOrderInfos();
   // code ...
}
  • Yes, it's working, thanks ! I think that Magento is pretty complicated compared to Symfony :/ (MVC talking) – Morgan Tartreau May 31 '18 at 13:43
3

Controller should be like: Here my define a registry variable order_infos this variable contain your data.To Know about how use registry variable checkout the blog: https://www.mageplaza.com/magento-2-registry-register.html

<?php

namespace {Vendor}\{ModuleName}\Controller;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\App\RequestInterface;

 class {ActioName} extends Action
{
    protected $directoryList;

   /**
    * @var \Magento\Framework\Registry
    */

    protected $_registry;

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Customer\Model\Session $customerSession
     */
    public function __construct(
        Context $context,
        CustomerSession $customerSession,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Filesystem\DirectoryList $directoryList
    ) {
        $this->customerSession = $customerSession;
        $this->_registry = $registry;
        $this->directoryList = $directoryList;
        parent::__construct($context);
    }

    /**
     * Check customer authentication for some actions
     *
     * @param \Magento\Framework\App\RequestInterface $request
     * @return \Magento\Framework\App\ResponseInterface
     */
    public function dispatch(RequestInterface $request)
    {
        if (!$this->customerSession->authenticate()) {
            $this->_actionFlag->set('', 'no-dispatch', true);
            if (!$this->customerSession->getBeforeUrl()) {
                $this->customerSession->setBeforeUrl($this->_redirect->getRefererUrl());
            }
        }
        return parent::dispatch($request);
    }

    public function execute()
    {

        $codeClient =$this->customerSession->getCustomer()->getCodeClient();
        $firstName  = $this->customerSession->getCustomer()->getFirstname();
        $lastname   = $this->customerSession->getCustomer()->getLastname();

        $fullName = strtolower($lastname.'_'.$firstName) ;
        $orderInfos = $this->getClientOrder($codeClient, $fullName);

        if($this->registry->registry('order_infos'))
        {
          $this->_registry->unregister('order_infos');
        }
        $this->registry->register('order_infos', $orderInfos );


        $resultPage = $this->_resultPageFactory->create();
        return $resultPage;
    }

    public function getClientOrder($code, $name){

        try{
            $base_path  =  $this->directoryList->getRoot();
            $fichier = 'fichier.xml';
            $xml = simplexml_load_file($fichier);
            $codeXML = $xml->name;
                return $codeXML;
        }catch (\Exception $e){

        }

        return false;
    }      
}

As describe by Evgeniy Kapelko, you need to define a layout,a block. At block class, you need to call this registry to get registry data

2

You can easily achieve it by using Block. Use Magento registry system to communicate between Controller and Blocks. Set any custom variable in registry with your Order data, and fetch the same data in any function of your Block class. In your template, just call the function defined in the Block and you are done.

  • Please could you show me an example ? – Morgan Tartreau May 30 '18 at 14:52
  • You can refer Magento Catalog module. The best practise is to look into Magento core files first. Refer the controller class where Magento sets "current_category" when we are at category pages: Magento\Catalog\Controller\Category\View: execute() Line no: 132 In the block class, Magento is fetching the same value at following line: Magento\Catalog\Block\Category\View::getCurrentCategory() Line no: 111 Now using this function, any one can access the values set as "current_category". – Sumit Verma May 30 '18 at 15:41

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.