Yii2 Ajax Popup Form - BaseUrl Class - Scheme

Scenario:

  1. Customer form

  2. Order Form

Loading Customer create form (customer/create) in order form via Ajax popup dialog. The action of the customer create form will be “create” ( jQuery(’#customer-create).attr(“action”) )

The form will be submitted to order/create instead of customer/create.

Solution 1:


ActiveForm::begin(['action' => Url::to('customer/create', true ]);

The second parameter of Url::to determines whether to return relative url or absolute url. True means absolute URL.

Solution 2: Override core helper

Copy vendor/yiisoft/yii2/helpers/BaseUrl.php to components ( common/components or @app/components )

Then change


public static function to($url = '', $scheme = false)

to


public static function to($url = '', $scheme = true)

The add the following code to entry script web/index.php


Yii::$classMap['yii\helpers\BaseUrl'] = '@common/components/BaseUrl.php';

Or


Yii::$classMap['yii\helpers\BaseUrl'] = '@app/components/BaseUrl.php';