1

I have doubt on object manager someone saying You should not use the ObjectManager directly! but I have questioned if I want to get customer session in my phtml how can I get?. that time can I use object manager in that file create customer session and get customer data.

And if need to filter my custom collection for that can use object manager or can I create an object in the block and call that function into phtml file to send filter value.

Can anyone tell coding standards?

  • On which page, you want? – Amit Bera Jun 5 '19 at 11:53
  • any custom page,i have developed custom module it related customer account section need to check customer login and customer group based on that need to display one right side link – Pawankumar Jun 5 '19 at 12:00
  • you want this page will show only on one page? – Amit Bera Jun 5 '19 at 12:11
0

Something like this

Customer.php

namespace Xigen\Session\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Customer extends AbstractHelper
{
    protected $customerSession;

    /**
     * @param \Magento\Framework\App\Helper\Context $context
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->customerSession = $customerSession;
        parent::__construct($context);
    }

    public function getCustomerId()
    {
        // current customer id
        return $this->customerSession->getCustomer()->getId();
    }
}

Then in template

$helper = $this->helper('\Xigen\Session\Helper\Customer');
echo $helper->getCustomerId()

Template logic will need to be in uncached block to function properly

0

You can depend on and use the ObjectManager class in the following scenarios:

  • You can use the object manager in static magic methods like __wakeup(), __sleep(), etc.
  • An example can be found in the __wakeup() method in the Magento/Eav/Model/Entity/Attribute/AbstractAttribute class.
  • You can use the ObjectManager to maintain backward compatibility for a constructor.
  • In a global scope, like in fixtures of integration tests, you can use the object manager.
  • The object manager can be a dependency in classes used for the creation of objects, e.g. factories or proxies.

Reference Magento 2 Official Docs

In nutshell al dependencies(except above cases) must be injected in the constructor. Dependency Injection will do the work. Then you will do specialised methods that return what you need.

More specifically in the scenario of providing data to templates, Magento recommends using ViewModels which are nothing but service classes that provide needed information in templates. These view models should be set in the layout files. see docblock of Magento\View\Framework\Element\Template

* Standard Magento block.
 * Should be used when you declare a block in frontend area layout handle.
 *
 * Avoid extending this class.
 *
 * If you need custom presentation logic in your blocks, use this class as block, and declare
 * custom view models in block arguments in layout handle file.
 *
 * Example:
 * <block name="my.block" class="Magento\Backend\Block\Template" template="My_Module::template.phtml" >
 *      <arguments>
 *          <argument name="viewModel" xsi:type="object">My\Module\ViewModel\Custom</argument>
 *      </arguments>
 * </block>

Such a ViewModel class is Magento\Catalog\ViewModel\Breadcrums.

Remember all classes that are passed through layout files must implement the Magento\Framework\View\Element\Block\ArgumentInterface

More on Dependency Injection in Magento can be found here

A good official reference of Magento coding standards can be found here

0

Customer data should be handled on the client and not the server, Magento include a JS library for handling this - see https://github.com/magento/magento2/blob/2.3/app/code/Magento/Customer/view/frontend/web/js/customer-data.js

As a summary use UI components to render customer data. An example can be seen here - https://devdocs.magento.com/guides/v2.3/extension-dev-guide/cache/page-caching/private-content.html

Since private content is specific to individual users, it’s reasonable to handle it on the client (i.e., web browser).

Use our customer-data JS library to store private data in local storage, invalidate private data using customizable rules, and synchronize data with the backend.

  • Whilst I appreciate this is the magento answer how do you personally feel about customer data being in local storage? – Dominic Xigen Jun 5 '19 at 19:47
  • For me that depends on the data being stored, for the likes of the user's name, DOB, last login I feel it's ok but I would never store anything such as credit cards or passwords in there. The main drawback is any third party JS can access it, but if third party JS gets compromised them accessing local storage is the least of my worries. My idea why there are so many posts on here about this topic is the huge majority of Magento devs are backend and aren't as comfortable with JS or UI components thus they want to achieve this with PHP. – Ben Crook Jun 5 '19 at 21:03
  • There are other topics that suffer from this back-end heavy mindset such as layout XML where it's pretty common to see answers advising people to load blocks or templates in PHP rather than layout XML. – Ben Crook Jun 5 '19 at 21:03
  • Also loading this data client-side has the additional benefit of bypassing cache, I've seen many bugs related to customer data being cached. Other platforms handle user data this way such as Shopware. – Ben Crook Jun 5 '19 at 21:09
  • 1
    Thank you for an interesting insight. You called it. I'm a backend dev. I was shown this recently on checkout and had similar understanding and concerns. But it's good to question what you know from time to time. – Dominic Xigen Jun 5 '19 at 21:39

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.