The observer is a class
same as any other class
in Magento and can be initialized via Dependency Injection in the constructor or via ObjectManager (not preferred).
For Example:
In your
MyVendor\MyModule\Observer\SaveObserver
<?php
namespace MyVendor\MyModule\Observer;
use Magento\Framework\Event\ObserverInterface;
class SaveObserver implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
try {
// Do something
} catch (\Exception $e) {
// silence
}
}
public function abc($order,$itemIds) {
// Your public function
}
}
In your
MyVendor\MyModule\Controller\Adminhtml\Help\Index
<?php
namespace MyVendor\MyModule\Controller\Adminhtml\Help;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use MyVendor\MyModule\Observer\SaveObserver;
/**
* Class Index
*/
class Index extends Action
{
/**
* ResultPageFactory
* @var PageFactory
*/
public $resultPageFactory;
public $observer;
/**
* Index constructor.
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory,
SaveObserver $observer
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
$this->observer = $observer;
}
/**
* @return \Magento\Backend\Model\View\Result\Page
*/
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$order = null;
$itemIds = null;
$this->observer->abc($order, $itemIds);
return $resultPage;
}
}