Yii2来自不同控制器

时间:2018-03-16 05:31:27

标签: yii2 yii2-basic-app

是否可以在不同视图中从控制器调用操作?

示例

我有2个控制器:发布博客,所以我想从帖子中调用actionCreate但在博客视图中不在帖子视图中。我有2个观看次数和2个控制器

view :
 1. views/blog/view
 2. views/post/view

controller
 1. controllers/blogController.php
 2. controllers/postController.php

controllers / PostController.php:

public function actionCreate()
{
    $model_Post = new Post();

    if ($model_Post->load(Yii::$app->request->post()) && $model_Post->save()) {
        return $this->redirect(['view', 'id' => $model_Post->Post_id]);
    } else {
        return $this->render('/blog/view', [
            'model_Post' => $model_Post,
        ]);
    }
}

视图/博客/ view.php

<?php

use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model app\models\Likectt */

$this->title = $model->Blog_id;

?>
<div class="blog-view">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a('Update', ['update', 'id' => $model->Blog_id], ['class' => 'btn btn-primary']) ?>
        <?= Html::a('Delete', ['delete', 'id' => $model->Blog_id], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Are you sure you want to delete this item?',
                'method' => 'post',
            ],
        ]) ?>
    </p>

    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'Blog_id',
            'Blog_title',
            'Blog_text',
            'User_id',
            'Category_id',
        ],
    ]) ?>

    <?= Yii::$app->runAction('PostController/actionCreate', ['model_Post'=>$model_Post]);?>

</div>

1 个答案:

答案 0 :(得分:1)

是的,你可以这样做:

在博客视图中:

public List getcourseByName(String courseName) {
    List<Coursedetails> courselist = null;
    try
    {
        Session session = sessionFactory.openSession();
        org.hibernate.Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Coursedetails.class)
                .add(Restrictions.eq("coursename", courseName));
        courselist = (List<Coursedetails>) criteria.list();
        tx.commit();