This on going updated post lists all calls to coreRegistry->register()
in Magento2. A registry cheat sheet.
Current version: 2.0.13 / Feb 13, 2017
Why is this useful?
If you want to extend any block, model, helper or listen to a specific event you might likely need to use the current product, category, cms page, cms block, order, quote, etc. model.
I’ve seen a lot of bad written Magento1 code where developers instantiated over and over again new objects of already available objects only to access a few properties. This slows down everything.
After you have identified your event or figured out where to integrate the plugin for the integration calls you should figure out the current route and check here if the main object is globally available in the registry.
The registry acts as a message channel between different objects/areas. It lets you communicate data from the controller to a helper, a block or another model. Of course you can communicate in any direction.
If you want to create registry entries for your modules, please do not abuse the registry for simple values. Create your own objects and share them via singleton patterns.
How can I use this feature in my code?
Here is an example code on how to use the coreRegistry. This fictional class creates new tabs in the catalog/category section. Each tab lists the best sold products per category.
The registry entry will be created here: app/code/Magento/Catalog/Controller/Adminhtml/Category.php#L67
namespace MyNamespace\MyModule\Block\Adminhtml\CategorySales;
class BestSalesTabs extends \Magento\Backend\Block\Widget\Tabs
{
/**
* Core registry
*
* @var \Magento\Framework\Registry|null
*/
protected $_coreRegistry = null;
/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
* @param \Magento\Backend\Model\Auth\Session $authSession
* @param \Magento\Framework\Registry $registry
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
\Magento\Backend\Model\Auth\Session $authSession,
\Magento\Framework\Registry $registry,
array $data = []
) {
$this->_coreRegistry = $registry;
parent::__construct($context, $jsonEncoder, $authSession, $data);
}
/**
* Retrieve category object
*
* @return \Magento\Catalog\Model\Category
*/
public function getCategory()
{
return $this->_coreRegistry->registry('current_category');
}
/**
* Prepare Layout Content
*
* @return $this
*/
protected function _prepareLayout()
{
$categoryAttributes = $this->getCategory()->getAttributes();
if (!$this->getCategory()->getId()) {
...
Do NOT access the registry via the objectManager:
$this->_objectManager->get('Magento\Framework\Registry')->registry('current_category');
This is an anti-pattern.
The usage of objectManager as service locator leads to highly coupled code and hidden dependencies. Also the ability to configure specific arguments for the client that uses the OM directly is lost.
The OM can be used only in composition root of application (Bootstrap) and for unserialization of entities with service dependencies.
Please use your browsers search function!
File / Code |
---|
app/code/Magento/Authorizenet/Controller/Adminhtml/Authorizenet/Directpost/Payment/Redirect.php NO_MATCH
|
app/code/Magento/Authorizenet/Controller/Directpost/Payment/Redirect.php NO_MATCH
|
app/code/Magento/Authorizenet/Controller/Directpost/Payment.php NO_MATCH
|
app/code/Magento/Authorizenet/Observer/SaveOrderAfterSubmitObserver.php directpost_order
|
app/code/Magento/Backend/Controller/Adminhtml/System/Design/Edit.php design
|
app/code/Magento/Backend/Controller/Adminhtml/System/Store/EditGroup.php store_type
|
app/code/Magento/Backend/Controller/Adminhtml/System/Store/EditStore.php store_post_data
|
app/code/Magento/Backend/Controller/Adminhtml/System/Store/EditStore.php store_type
|
app/code/Magento/Backend/Controller/Adminhtml/System/Store/EditStore.php store_action
|
app/code/Magento/Backend/Controller/Adminhtml/System/Store/EditStore.php store_data
|
app/code/Magento/Backend/Controller/Adminhtml/System/Store/EditWebsite.php store_type
|
app/code/Magento/Backend/Controller/Adminhtml/System/Store/NewGroup.php store_type
|
app/code/Magento/Backend/Controller/Adminhtml/System/Store/NewStore.php store_type
|
app/code/Magento/Backend/Controller/Adminhtml/System/Store/NewStore.php store_action
|
app/code/Magento/Backend/Controller/Adminhtml/System/Store/NewWebsite.php store_type
|
app/code/Magento/Backup/Controller/Adminhtml/Index/Create.php backup_manager
|
app/code/Magento/Backup/Controller/Adminhtml/Index/MassDelete.php backup_manager
|
app/code/Magento/Backup/Controller/Adminhtml/Index/Rollback.php backup_manager
|
app/code/Magento/Backup/Cron/SystemBackup.php backup_manager
|
app/code/Magento/Bundle/Model/Product/CatalogPrice.php rule_data
|
app/code/Magento/Catalog/Block/Adminhtml/Category/AbstractCategory.php root
|
app/code/Magento/Catalog/Block/Adminhtml/Category/AbstractCategory.php root
|
app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Main.php attribute_type_hidden_fields
|
app/code/Magento/Catalog/Block/Product/View.php product
|
app/code/Magento/Catalog/Controller/Adminhtml/Category/Widget/CategoriesJson.php category
|
app/code/Magento/Catalog/Controller/Adminhtml/Category/Widget/CategoriesJson.php current_category
|
app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Edit.php entity_attribute
|
app/code/Magento/Catalog/Controller/Adminhtml/Product/Set/Edit.php current_attribute_set
|
app/code/Magento/Catalog/Controller/Adminhtml/Product/Set.php entityType
|
app/code/Magento/Catalog/Controller/Category/View.php current_category
|
app/code/Magento/Catalog/Helper/Product/Composite.php composite_update_result
|
app/code/Magento/Catalog/Helper/Product/Composite.php current_product
|
app/code/Magento/Catalog/Helper/Product/Composite.php product
|
app/code/Magento/Catalog/Helper/Product/Composite.php NO_MATCH
|
app/code/Magento/Catalog/Helper/Product/Composite.php composite_configure_result_error_message
|
app/code/Magento/Catalog/Helper/Product.php current_category
|
app/code/Magento/Catalog/Helper/Product.php current_product
|
app/code/Magento/Catalog/Helper/Product.php product
|
app/code/Magento/Catalog/Model/Layer/Filter/DataProvider/Category.php current_category_filter
|
app/code/Magento/Catalog/Model/Product/Attribute/Source/Inputtype.php attribute_type_hidden_fields
|
app/code/Magento/Catalog/Model/Product/Type/AbstractType.php used_super_product_
|
app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/Edit.php current_promo_catalog_rule
|
app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Widget/CategoriesJson.php category
|
app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Widget/CategoriesJson.php current_category
|
app/code/Magento/CheckoutAgreements/Controller/Adminhtml/Agreement/Edit.php checkout_agreement
|
app/code/Magento/Cms/Controller/Adminhtml/Block/Edit.php cms_block
|
app/code/Magento/Cms/Controller/Adminhtml/Page/Edit.php cms_page
|
app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images.php storage
|
app/code/Magento/Config/Model/Config/Source/Email/Template.php config_system_email_template
|
app/code/Magento/Customer/Block/Adminhtml/Edit/Tab/Newsletter.php subscriber
|
app/code/Magento/Customer/Controller/Adminhtml/Group/NewAction.php NO_MATCH
|
app/code/Magento/Customer/Controller/Adminhtml/Index/Newsletter.php subscriber
|
app/code/Magento/Customer/Controller/Adminhtml/Index/Save.php NO_MATCH
|
app/code/Magento/Customer/Controller/Adminhtml/Index.php NO_MATCH
|
app/code/Magento/Customer/Observer/AfterAddressSaveObserver.php NO_MATCH
|
app/code/Magento/Customer/Observer/AfterAddressSaveObserver.php NO_MATCH
|
app/code/Magento/Customer/Observer/BeforeAddressSaveObserver.php NO_MATCH
|
app/code/Magento/Customer/Observer/BeforeAddressSaveObserver.php NO_MATCH
|
app/code/Magento/Email/Controller/Adminhtml/Email/Template.php email_template
|
app/code/Magento/Email/Controller/Adminhtml/Email/Template.php current_email_template
|
app/code/Magento/Newsletter/Block/Adminhtml/Subscriber/Grid/Filter/Website.php website_collection
|
app/code/Magento/Newsletter/Controller/Adminhtml/Queue/Edit.php current_queue
|
app/code/Magento/Newsletter/Controller/Adminhtml/Template/Edit.php _current_template
|
app/code/Magento/Paypal/Controller/Adminhtml/Billing/Agreement/CustomerGrid.php NO_MATCH
|
app/code/Magento/Paypal/Controller/Adminhtml/Billing/Agreement.php current_billing_agreement
|
app/code/Magento/Paypal/Controller/Adminhtml/Paypal/Reports/Details.php current_transaction
|
app/code/Magento/Paypal/Controller/Billing/Agreement.php current_billing_agreement
|
app/code/Magento/Paypal/Controller/Transparent/Response.php NO_MATCH
|
app/code/Magento/Paypal/Observer/SaveOrderAfterSubmitObserver.php hss_order
|
app/code/Magento/Review/Block/Adminhtml/Edit.php ret
|
app/code/Magento/Review/Block/Adminhtml/Edit.php review_data
|
app/code/Magento/Review/Block/Adminhtml/Rating/Edit.php rating_data
|
app/code/Magento/Review/Controller/Adminhtml/Product/Pending.php usePendingFilter
|
app/code/Magento/Review/Controller/Adminhtml/Product/Pending.php usePendingFilter
|
app/code/Magento/Review/Controller/Adminhtml/Rating.php entityId
|
app/code/Magento/Review/Controller/Product/ListAction.php productId
|
app/code/Magento/Review/Controller/Product/View.php current_review
|
app/code/Magento/Review/Controller/Product.php current_category
|
app/code/Magento/Review/Controller/Product.php current_product
|
app/code/Magento/Review/Controller/Product.php product
|
app/code/Magento/Sales/Controller/AbstractController/PrintCreditmemo.php current_order
|
app/code/Magento/Sales/Controller/AbstractController/PrintCreditmemo.php current_creditmemo
|
app/code/Magento/Sales/Controller/AbstractController/PrintInvoice.php current_order
|
app/code/Magento/Sales/Controller/AbstractController/PrintInvoice.php current_invoice
|
app/code/Magento/Sales/Controller/AbstractController/PrintShipment.php current_order
|
app/code/Magento/Sales/Controller/AbstractController/PrintShipment.php current_shipment
|
app/code/Magento/Sales/Controller/Adminhtml/Order/Address.php order_address
|
app/code/Magento/Sales/Controller/Adminhtml/Order/Status/Edit.php current_status
|
app/code/Magento/Sales/Controller/Adminhtml/Order/Status/NewAction.php current_status
|
app/code/Magento/Sales/Controller/Adminhtml/Order.php sales_order
|
app/code/Magento/Sales/Controller/Adminhtml/Order.php current_order
|
app/code/Magento/Sales/Controller/Adminhtml/Transactions.php current_transaction
|
app/code/Magento/Sales/Controller/Guest/PrintCreditmemo.php current_creditmemo
|
app/code/Magento/Sales/Controller/Guest/PrintInvoice.php current_invoice
|
app/code/Magento/Sales/Controller/Guest/PrintShipment.php current_shipment
|
app/code/Magento/Sales/Helper/Guest.php current_order
|
app/code/Magento/Sales/Model/AdminOrder/Create.php rule_data
|
app/code/Magento/SalesRule/Controller/Adminhtml/Promo/Quote/Edit.php current_promo_quote_rule
|
app/code/Magento/SalesRule/Controller/Adminhtml/Promo/Quote.php current_promo_quote_rule
|
app/code/Magento/Search/Controller/Adminhtml/Term/Edit.php current_catalog_search
|
app/code/Magento/SendFriend/Controller/Product/Sendmail.php current_category
|
app/code/Magento/SendFriend/Controller/Product.php product
|
app/code/Magento/Shipping/Controller/Tracking/Popup.php current_shipping_info
|
app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Edit.php sitemap_sitemap
|
app/code/Magento/Tax/Controller/Adminhtml/Rate/Add.php NO_MATCH
|
app/code/Magento/Tax/Controller/Adminhtml/Rate/Edit.php NO_MATCH
|
app/code/Magento/Tax/Controller/Adminhtml/Rule/Edit.php tax_rule_id
|
app/code/Magento/Tax/Controller/Adminhtml/Rule/Edit.php tax_rule_form_data
|
app/code/Magento/Theme/Controller/Adminhtml/System/Design/Theme/Edit.php current_theme
|
app/code/Magento/User/Block/Role/Grid/User.php RID
|
app/code/Magento/User/Controller/Adminhtml/User/Edit.php permissions_user
|
app/code/Magento/User/Controller/Adminhtml/User/Role.php current_role
|
app/code/Magento/User/Controller/Adminhtml/User/RolesGrid.php permissions_user
|
app/code/Magento/Variable/Controller/Adminhtml/System/Variable.php current_variable
|
app/code/Magento/Widget/Controller/Adminhtml/Widget/Index.php skip_widgets
|
app/code/Magento/Widget/Controller/Adminhtml/Widget/Instance.php current_widget_instance
|
app/code/Magento/Wishlist/Controller/Index/Configure.php wishlist_item
|
app/code/Magento/Braintree/Model/PaymentMethod.php NO_MATCH
|
app/code/Magento/Catalog/Model/Category.php _category_is_anchor_attribute
|
app/code/Magento/CatalogSearch/Model/Advanced.php advanced_search_conditions
|
app/code/Magento/Config/Model/Config/Backend/Admin/Custompath.php custom_admin_path_redirect
|
app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php NO_MATCH
|
app/code/Magento/Integration/Controller/Adminhtml/Integration/Delete.php NO_MATCH
|
app/code/Magento/Integration/Controller/Adminhtml/Integration/Edit.php NO_MATCH
|
app/code/Magento/Integration/Controller/Adminhtml/Integration/NewAction.php NO_MATCH
|
app/code/Magento/Integration/Controller/Adminhtml/Integration/PermissionsDialog.php NO_MATCH
|
app/code/Magento/Integration/Controller/Adminhtml/Integration/TokensDialog.php NO_MATCH
|
app/code/Magento/Catalog/Controller/Adminhtml/Category.php category
|
app/code/Magento/Catalog/Controller/Adminhtml/Category.php current_category
|