Magento 2 Registry is a class that is used to share the data between objects in Magento. 

e.g. save the object to the Registry in the controller class and get in the block class.

Starting from Magento 2.3 the Registry class was declared as deprecated but a lot of developers, extension vendors, and even Magento core code still use it.

How does it work?

To get a Registry object in your class you need to define it in the constructor, for example:

/**
* @var \Magento\Framework\Registry
*/
private $registry;

/**
* ...
* @param \Magento\Framework\Registry $registry,
*/
public function __construct(
...,
\Magento\Framework\Registry $registry,
...
) {
$this->registry = $registry;
...
}

or you can easily get it via the Object Manager for the testing purpose:

$registry = \Magento\Framework\App\ObjectManager::getInstance()
   ->get(\Magento\Framework\Registry::class);

How to SET a new value into the Registry?

To set a new value into the register please use the public function register($key, $value, $graceful = false) method.

Example:

$this->registry->register('my_value', 5);

If you don't set the parameter $graceful or its value is false and the key already exists in the Registry, then you will get an error "'Registry key  {{key_name}}  already exists'".

How to GET a value from the Registry?

To get a value from the register please use the public function registry($key) method.

Example:

$this->registry->registry('my_value');

If the key does not exist in the Register it will return null.

How to REMOVE a value from the Registry?

To remove a value from the register please use the public function unregister($key) method.

Example:

$this->registry->unregister('my_value');

If the key does not exist in the Register it will simply do nothing.

Magento 2 Registry Usage Examples

Now let's review some of the most common examples.

How to get a current PRODUCT in Magento?

public function getCurrentProduct()
{
    /* @var \Magento\Framework\Registry */
    return $this->registry->registry('current_product');
}

How to get a current CATEGORY in Magento?

public function getCurrentCategory()
{
    /* @var \Magento\Framework\Registry */
    return $this->registry->registry('current_category');
}

How to get a current CMS PAGE in Magento?

public function getCurrentPage()
{
    /* @var \Magento\Framework\Registry */
    return $this->registry->registry('current_cms_page');
}

How to get a current Magefan BLOG POST in Magento?

public function getCurrentPost()
{
    /* @var \Magento\Framework\Registry */
    return $this->registry->registry('current_blog_post');
}

How to get a current Magefan BLOG CATEGORY in Magento?

public function getCurrentBlogCategory()
{
    /* @var \Magento\Framework\Registry */
  return $this->registry->registry('current_blog_category');
}

How to get a current Magefan BLOG TAG in Magento?

public function getCurrentTag()
{
    /* @var \Magento\Framework\Registry */
    return $this->registry->registry('current_blog_tag');
}

How to get a current Magefan BLOG AUTHOR in Magento?

public function getCategory()
{
    /* @var \Magento\Framework\Registry */
    return $this->registry->registry('current_blog_author');
}