在Yii2中将新网页添加到您网站的后端

时间:2018-06-01 19:29:56

标签: yii2-advanced-app

我在这里感到困惑。
到目前为止,我添加了一个新的 controller:Policyread.php

    <?php
    namespace backend\controllers;

    use Yii;
    use yii\web\Controller;
    use yii\filters\VerbFilter;
    use yii\filters\AccessControl;
    use yii\web\NotFoundHttpException;
    use backend\models\Policyread;
    use backend\models\PolicyreadSearch;

    /**
     * PolicyreadController implements the CRUD actions for Policyread model.
     */
    class PolicyreadController extends Controller
    {
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                'access' => [
                    'class' => AccessControl::className(),
                    'rules' => [
                        [
                            'actions' =>['index','training','unread'],
                            'allow'=> true,
                        ],
                    ],
                ],
                'verbs' => [
                    'class' => VerbFilter::className(),
                    'actions' => [
                        'delete' => ['POST'],

                    ],
                ],
            ];
        }

        /**
         * Lists all Policyread models.
         * @return mixed
         */
        public function actionIndex()
        {
            $searchModel = new PolicyreadSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
            //$policyread = new Policyread;

            //return $this->render('index', [
            return $this->render('training', [
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,
                //'policyread' => $policyread,
            ]);
        }

        /**
         * Displays a single Policyread model.
         * @param string $id
         * @return mixed
         * @throws NotFoundHttpException if the model cannot be found
         */
        public function actionView($id)
        {
            return $this->render('view', [
                'model' => $this->findModel($id),
            ]);
        }

        /**
         * Creates a new Policyread model.
         * If creation is successful, the browser will be redirected to the 'view' page.
         * @return mixed
         */
        public function actionCreate()
        {
            $model = new Policyread();

            if ($model->load(Yii::$app->request->post()) && $model->save()) {

                $model->read_date = Date('Y-m-d H:i:s');
                $model->save();

                return $this->redirect(['view', 'id' => $model->pr_id]);
            }

            return $this->render('create', [
                'model' => $model,
            ]);
        }

        /**
         * Updates an existing Policyread model.
         * If update is successful, the browser will be redirected to the 'view' page.
         * @param string $id
         * @return mixed
         * @throws NotFoundHttpException if the model cannot be found
         */
        public function actionUpdate($id)
        {
            $model = $this->findModel($id);

            if ($model->load(Yii::$app->request->post()) && $model->save()) {

                return $this->redirect(['view', 'id' => $model->pr_id]);
            }

            return $this->render('update', [
                'model' => $model,
            ]);
        }

        /**
         * Deletes an existing Policyread model.
         * If deletion is successful, the browser will be redirected to the 'index' page.
         * @param string $id
         * @return mixed
         * @throws NotFoundHttpException if the model cannot be found
         */
        public function actionDelete($id)
        {
            $this->findModel($id)->delete();

            return $this->redirect(['index']);
        }



        /************************
        /** CUSTOM CONTROLLERS **
        /***********************/

        public function actionTraining($id)
        {
            $searchModel = new PolicyreadSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
            //$policyread = new Policyread;

            return $this->render('training', [
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,
                'model' => $this->findModel($id),
               // 'policyread' => $policyread,
            ]);
        }

        public function actionUnread($id)
        {
            $model = $this->findModel($id);

            $searchModel = new PolicyreadSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
            //$policyread = new Policyread;

            return $this->render('unread', [
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,
                'model' => $model,
                //'policyread' => $policyread,
            ]);
        }



        /**
         * Finds the Policyread model based on its primary key value.
         * If the model is not found, a 404 HTTP exception will be thrown.
         * @param string $id
         * @return Policyread the loaded model
         * @throws NotFoundHttpException if the model cannot be found
         */
        protected function findModel($id)
        {
            if (($model = Policyread::findOne($id)) !== null) {
                return $model;
            }

            throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
        }


        public function actionLists($id)
        {
    //        $countDepartments = Department::find()
    //            ->where(['company_id' => $id])
    //            ->andWhere(['branch_id' => $id])
    //            ->count();
    //
    //        $departments = Department::find()
    //            ->where(['company_id' => $id])
    //            ->andFilterWhere(['branch_id' => $id])
    //            ->all();
    //
    //        if ($countDepartments > 0) {
    //            foreach ($departments as $department) {
    //                echo "<option value='" . $department->department_id . "'>" . $department->department_name . "</option>";
    //            }
    //        } else {
    //            echo "<option>There are no Departments</option>";
    //        }
        }

        /**
         * *Displays a single Address model as a single AJAX response
         * @param interger $id
         * @return mixed
         */
        public function actionAjaxView($id)
        {
            return $this->renderPartial('_view', [
                'model' => $this->findModel($id),
            ]);
        }

        public function actionAjaxViewUnread($id)
        {
            return $this->renderPartial('_viewUnread', [
                'model' => $this->findModel($id),
            ]);
        }



        public function actionUnderstood($id)
        {
    //        $model = $this->findModel($id);
    //
    //        if ($model->load(Yii::$app->request->post()) && $model->save()) {
    //
    //            $model = $this->findModel($id);
    //            $model->user_id = $this->$user_id;
    //            $model->policy_id = $policy_id;
    //            $model->ps_id = '1';
    //            $model->read_date = Date ('Y-m-d H:i:s');
    //            $model->save();
    //
    //            return $this->redirect(['view', 'id' => $model->policy_id]);
    //        }
    //
    //        return $this->render('update', [
    //            'model' => $model,
    //        ]);
        }
    }

模型有Policyread.php,PolicyreadSearch view有index.php ...和training.php

    <?php

    use yii\helpers\Html;
    use yii\grid\GridView;
    use yii\widgets\Pjax;
    use backend\models\Policy;
    use backend\models\Policyread;

    //$this->registerJs("
    //$('div.policytrainer-index').on('click','tr',function(){
    //var id =$(this).data('key');
    //$.ajax({
    //'type' : 'GET',
    //'url' : '$ajax_url',
    //'dataType' : 'html',
    //'data' : {
    //        '$csrf_param' : '$csrf_token',
    //        'id' : id
    //},
    //'success' : function(data){
    //        $('#policytrainer-detail').html(data);
    //}
    //});
    //});
    //");


    /* @var $this yii\web\View */
    /* @var $searchModel backend\models\PolicyreadSearch */
    /* @var $dataProvider yii\data\ActiveDataProvider */

    $msg = Yii::$app->user->identity->username . 's\' up to date policies';

    $this->title = Yii::t('app', $msg );
    $this->params['breadcrumbs'][] = $this->title;
    ?>
    <div class="policytrainer-index">

        <h1><?= Html::encode($this->title) ?></h1>
        <?php Pjax::begin(); ?>
        <?php //echo $this->render('_search', ['model' => $searchModel]); ?>

        <p>
            <?php //echo Html::a(Yii::t('app', 'Create Policy Read'), ['create'], ['class' => 'btn btn-success']) ?>
        </p>
        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'columns' => [
                // ['class' => 'yii\grid\SerialColumn'],

                // 'pr_id',
                //'user_id',
                //'user.username',

                'policypack.package',
                'policy.title',
                'read_date',

                //['class' => 'yii\grid\ActionColumn', 'template' => '{view} {update} {delete} '],
            ],
        ]); ?>
        <?php Pjax::end(); ?>
    </div>

    <div id="policytrainer-detail">
        <?php // echo $this->render('_view', ['model' => $policyread]); ?>
    </div>

当我转到访问培训页面时,我收到404错误..我已更新控制器以确认actionTraining。

我在这里缺少什么..我能够添加一个&#34; helloworld&#34;页面到站点文件夹,它工作,但这个由于某种原因不起作用。 我在这里缺少什么在我的上一次构建中它运行良好,但我通过将actionIndex路由到培训而不是index.php来攻击它。 关心Golith

0 个答案:

没有答案