Events in Yii2 when to use?


in order to make my code reusable and decouple application into modules I decided to use events.

For example I have a controller:

class InvoiceController extends Controller {

 public function actionAddinvoice() {

    $InvoiceEvent = new InvoiceEvent();

    //here I generate new invoice number


    $invoiceNumber = $InvoiceEvent->getInvoiceNumber();



and my InvoiceEvent class:

class InvoiceEvent extends Component {

   private $invoiceNumber;


   public function getInvoiceNumber(){



   $this->$invoiceNumber = ....



Suppose that the function getInvoiceNumber() has a lot to do (check db, prepare number etc.), so I remove it from controller to keep it thin. Should I use events this way? Is that correct and has sense?