【问题标题】:Yii2 - How to render the index view plus the create view all in the same pageYii2 - 如何在同一页面中呈现索引视图和创建视图
【发布时间】:2014-12-30 00:06:11
【问题描述】:

您好,

到目前为止,我已经让 Yii2 应用程序总是在某些控制器中为每个操作呈现一个视图。

但现在我需要在同一个屏幕上渲染 2 个视图文件,indexcreate

Yii1.xx 里有renderPartial(),Yii2 现在有render() 代替但是不知道怎么用。我知道语法有点像app/Controller::render(),但它并没有给我敲响警钟。

我的 SiteController 中的 actionIndex 是:

public function actionIndex()
{
    $searchModel = new TubeSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->redirect(Url::toRoute('tube/index'), [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

此操作会将试管/索引页面加载为应用的起始页面。我想加入加载创建操作。是否有可能 -> 两个 php 文件在 statup 的同一屏幕上呈现。

非常感谢...

【问题讨论】:

  • 我没有得到 -1 的标记点,这是一个对我来说很重要的问题需要解决。我已经对该主题进行了研究,并且已经在研究它,但是到目前为止根本无法获得可以满足我需要的解决方案。所以我写了这篇文章来寻求帮助。

标签: views render yii2


【解决方案1】:

从您的问题中不清楚您实际上想要实现什么,因此我将根据两个非常不同的场景提出两种解决方案。在这两种情况下,您都需要将表单的action参数配置为指向tube/create,否则您的表单将无法正确提交。

场景 1 - 您希望在用户访问 site/index 时呈现 tube/index 视图

由于视图 tube/index 似乎是关于创建新模型,为了简单起见,我将其命名为 tube/create。

在我看来,从您的索引页面重定向似乎是一种不好的做法。这将使您的服务器上的负载加倍,并且用户可能会对他们被重定向的原因感到困惑。实现这一点的最简单方法就是在站点控制器的index 操作中呈现tube/create 视图,如下所示;

public function actionIndex(){

    $searchModel = new TubeSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    $model = new Tube();

    $model->load(Yii::$app->request->post());
    $model->save();
    return $this->render('@app/views/tube/create', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
        'model' => $model,
    ]);

}

场景 2 - 您想在同一页面上呈现两个视图,一个用于查看某种搜索的结果,另一个用于创建新模型。这就是你可能在 Yii1 中使用的renderPartial()。你会这样做;

我坚持在站点/索引操作中使用渲染管/创建。

public function actionIndex(){

    $searchModel = new TubeSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    $model = new Tube();

    $model->load(Yii::$app->request->post());
    $model->save();
    return $this->render('//tube/index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
        'model' => $model,
    ]);

}

然后,您只需在 tube/index 视图文件中渲染两个单独的局部视图,如下所示。

/** tube/index **/
echo $this->render('//tube/view', [
    'searchModel' => $searchModel,
    'dataProvider' => $dataProvider,]);
echo $this->render('//tube/create', [
    'model' => $model]
);

这两种情况假设您的视图文件位于views/tube 文件夹中,并且您已经加载了必要的模型,并在控制器文件的顶部使用use 语句。

【讨论】:

    【解决方案2】:

    我自己解决了。

    站点控制器代码:

    public function actionIndex()
    {
        return $this->redirect(Url::toRoute('tube/index'));
    }
    

    管控制器代码:

    public function actionIndex()
    {
        $searchModel = new TubeSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $model = new Tube();
    
        if ($model->load(Yii::$app->request->post())) {
            $model->save();
        }
    
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'model' => $model,
        ]);
    
    }
    

    这样索引和创建视图在同一个页面中,一切正常。

    【讨论】:

      猜你喜欢
      • 2017-03-31
      • 2014-02-09
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      相关资源
      最近更新 更多