Create a new object using Object Manager in Magento
Free Preview
Learn how to create Magento objects with Object Manager and access customer session data programmatically.
Lesson Content
Rather than using the new keyword to create objects in Magento, the Object Manager class can be used. Object Manager is the dependency injection container class, and it will create objects that are instances of whatever class we pass to it.
This lesson is for informational purposes only. You never really want to ever use the ObjectManager class directly like we are about to do. Doing so circumvents how objects are to be created with dependency injection, and is known as a very bad practice in Magento land. That said, let’s demonstrate how we would go about creating objects like this anyway, because there are very specific edge-cases where you may need to do this one day, and it also helps explain a bit how Magento works.
At the top of our execute() function, call the ObjectManager class from Magento\Framework\App. Call it statically with double colons, grab an instance of Object Manager with getInstance(), and assign it to a variable named $om.
Here’s the updated controller with Object Manager pulled in and instantiated:
<?php
declare(strict_types=1);
namespace Macademy\Blog\Controller\Post;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\ObjectManager;
class Detail implements HttpGetActionInterface
{
public function execute()
{
$om = ObjectManager::getInstance();
echo '<pre>';
var_dump(new Session());
}
}
Next, use this Object Manager instance to get an instance of the session. Call its get() function and pass in the class constant of the Magento\Customer\Model\Session class, which is really just the fully-qualified string of the class name.
Now the $session variable is an instance of this Session class. You can call the session getData() method to get all of the data from the customer session.
<?php
declare(strict_types=1);
namespace Macademy\Blog\Controller\Post;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\ObjectManager;
class Detail implements HttpGetActionInterface
{
public function execute()
{
$om = ObjectManager::getInstance();
$session = $om->get(Session::class);
echo '<pre>';
var_dump($session->getData());
die();
}
}
When you refresh the page, you’ll see that you’re getting this data from the session.
<?php
declare(strict_types=1);
namespace Macademy\Blog\Controller\Post;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\ObjectManager;
class Detail implements HttpGetActionInterface
{
public function execute()
{
$om = ObjectManager::getInstance();
$session = $om->get(Session::class);
echo '<pre>';
var_dump($session->getData());
die();
}
}
<?php
declare(strict_types=1);
namespace Macademy\Blog\Controller\Post;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\HttpGetActionInterface;
+use Magento\Framework\App\ObjectManager;
class Detail implements HttpGetActionInterface
{
public function execute()
{
+ $om = ObjectManager::getInstance();
+ $session = $om->get(Session::class);
echo '<pre>';
- var_dump(new Session());
+ var_dump($session->getData());
die();
}
}
<?php
declare(strict_types=1);
namespace Vendor\Module\Controller\Post;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\ObjectManager;
class Detail implements HttpGetActionInterface
{
public function execute()
{
$om = ObjectManager::getInstance();
$session = $om->get(Session::class);
echo '<pre>';
var_dump($session->getData());
die();
}
}
Join the discussion!
Comments
Want to comment on this lesson?
Enroll in this course to leave comments.