Yii1.1迁移Yii2遇Undefined variable: filtersForm报错求助
Hey, let's break down the issues you're facing here and get them sorted step by step.
1. Core Issue: Missing Variables in Controller
The Undefined variable: filtersForm error happens because your actionIndex method in SiteController isn't passing any variables to the view, but your index.php view is trying to use $filtersForm, $exportScenario, $inDataProvider, and others when rendering partial views (in, out, missed).
Yii2 requires you to explicitly pass variables from the controller to the view via the second parameter of render(). Here's how to update your controller code:
/** * Displays homepage. * @return string */ public function actionIndex() { // Initialize all variables your view needs // Replace these with your actual model classes and business logic $filtersForm = new \app\models\FiltersForm(); // Your filter form model $exportScenario = 'export_incoming'; // Adjust to your actual scenario $exportScenario2 = 'export_outgoing'; $exportScenario3 = 'export_missed'; $date = date('Y-m-d'); // Or fetch from request/DB as needed $model = new \app\models\CallModel(); // Your main call model // Set up data providers for each call type $inDataProvider = new \yii\data\ActiveDataProvider([ 'query' => \app\models\CallModel::find()->where(['call_type' => 'incoming']), // Add pagination/sorting if needed ]); $outDataProvider = new \yii\data\ActiveDataProvider([ 'query' => \app\models\CallModel::find()->where(['call_type' => 'outgoing']), ]); $missedDataProvider = new \yii\data\ActiveDataProvider([ 'query' => \app\models\CallModel::find()->where(['call_type' => 'missed']), ]); // Pass all variables to the view return $this->render('index', [ 'filtersForm' => $filtersForm, 'exportScenario' => $exportScenario, 'exportScenario2' => $exportScenario2, 'exportScenario3' => $exportScenario3, 'date' => $date, 'model' => $model, 'inDataProvider' => $inDataProvider, 'outDataProvider' => $outDataProvider, 'missedDataProvider' => $missedDataProvider, ]); }
Make sure to replace the placeholder model classes (FiltersForm, CallModel) with your actual Yii2-compatible models (they should extend yii\base\Model for forms or yii\db\ActiveRecord for database models).
2. Yii1 to Yii2 Widget Replacement
Another critical issue: zii.widgets.CPortlet is a Yii1 component that doesn't exist in Yii2. You need to replace it with a Yii2 alternative. The most common replacement is using the Bootstrap Panel widget (if you're using Bootstrap in your Yii2 project):
Replace code like this:
$this->beginWidget ( 'zii.widgets.CPortlet', [ 'title' => '<span class="icon-picture"></span>Incoming Calls', 'titleCssClass' => '' ] ); // ... content ... $this->endWidget ();
With this Yii2-compatible code:
echo \yii\bootstrap\Panel::widget([ 'title' => '<span class="icon-picture"></span>Incoming Calls', 'titleOptions' => ['class' => ''], // Equivalent to titleCssClass 'content' => $this->render('in', [ 'filtersForm' => $filtersForm, 'exportScenario' => $exportScenario, 'date' => $date, 'model' => $model, 'inDataProvider' => $inDataProvider ]), ]);
If you're not using Bootstrap, you can create a custom HTML structure to mimic the Portlet style:
<div class="portlet"> <div class="portlet-title"> <span class="icon-picture"></span>Incoming Calls </div> <div class="portlet-content"> <?= $this->render('in', [/* variables */]) ?> </div> </div>
Then add CSS to style .portlet, .portlet-title, etc., to match your original design.
3. Quick Checks to Avoid Future Issues
- Double-check that all partial views (
in.php,out.php,missed.php) properly handle the variables you're passing to them. - Ensure all your models are correctly namespaced (Yii2 uses PSR-4 autoloading, so classes should live in directories like
app\models).
内容的提问来源于stack exchange,提问作者xbile

