I have introduce How to use Helper and setting in Magento 2 extension . I will introduce how to use event in Magento. Event in Magento is very importance in Magento 2 extension because some time you have to change data, do somethings before or after action from Magento core . It include tow parts :
1. Create Event
2 . Catch Event
1. Create Event
To create event, Create Text Event Action and enter code bellow
<?php namespace Magebay\Hello\Controller\Index; use Magento\Framework\App\Action\Context; use Magento\Framework\View\Result\PageFactory; /** * Class Index * @package Magebay\Hello\Controller\Index */ class TestEvent extends \Magento\Framework\App\Action\Action { protected $session; public function __construct( Context $context, PageFactory $resultPageFactory, \Magento\Customer\Model\Session $session ) { parent::__construct($context); $this->resultPageFactory = $resultPageFactory; $this->session = $session; } /** * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void */ public function execute() { $text = 'Hello '; $this->session->setTextMessage($text); $this->_eventManager->dispatch('magebay_hello_display_text_before', ['hello_message' => $text]); echo $this->session->getTextMessage(); } }
2. Catch Event.
Catch event is very importance because some time you have to custom data or action from core Magento . Example : Custom Price when adding product cart . Create event, you can do 2 step bellow:
Step 1 : create event in xml file . Create file app/code/Magebay/Hello/etc/frontend/events.xml.
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="magebay_hello_display_text_before"> <observer name="mb_show_text" instance="Magebay\Hello\Observer\DisplayText" /> </event> </config>
Note: if you want to Catch events in frontend, can create events in etc/fontend/events.xml, you if want to Catch events in backend, you can create event in etc/adminhtml/events.xml and if you want to the both area, create events in etc/events.xml
Step 2 : Create file app/code/Magebay/Hello/Observer/DisplayText.php
<?php /** * Created by PhpStorm. * User: maiuoc * Date: 2019-01-19 * Time: 4:58 PM */ namespace Magebay\Hello\Observer; use Magento\Framework\Event\Observer as EventObserver; use Magento\Framework\Event\ObserverInterface; class DisplayText implements ObserverInterface { protected $session; function __construct( \Magento\Customer\Model\Session $session ) { $this->session = $session; } public function execute(EventObserver $observer) { // TODO: Implement execute() method. $message = $observer->getData('hello_message'); $message .= ' Magebay'; // change text $this->session->setTextMessage($message); } }
If if focus the Tutorial , You can understand basic knowledge About Magento extension . I hope you can build a Magento extension by your self . if you have any question , you can comment under the post, I will check help you. I will introduce how to how to create admin grid in magento 2.