yii2 Session Remember URL to revisit later


Example

Use case: remember the current URL to return to after adding a new record in a different (related) controller, for instance create a new contact to add to an invoice being edited.

InvoiceController / actionUpdate:

Url::remember(Url::current(), 'returnInvoice');

ContactController / actionCreate:

if ($model->save()) {
    $return = Url::previous('returnInvoice');
    if ($return) {
        return $this->redirect($return);
    }
    // ...
}

You can reset the remembered URL once you're done:

InvoiceController / actionUpdate:

if ($model->save()) {
    Url::remember(null, 'returnInvoice');
    // ...
}

The key name - returnInvoice in this example - is optional.