BlogController.php 1.97 KB
<?php
    namespace frontend\controllers;

    use yii\data\ActiveDataProvider;
    use yii\web\Controller;
    use artbox\weblog\models\Article;
    use yii\web\NotFoundHttpException;

    /**
     * User: timur
     * Date: 26.01.18
     * Time: 8:46
     */
    
    class BlogController extends Controller
    {
        public function actionIndex()
        {
            
            $dataProvider = new ActiveDataProvider(
                [
                    'query' => Article::find()
                                      ->where(
                                          [
                                              'status' => true,
                                          ]
                                      ),
                    'pagination' => [
                        'pageSize' => 5,
                    ],
                ]
            );
            
            return $this->render(
                'index',
                [
                    'dataProvider' => $dataProvider,
                ]
            );
        }
        
        public function actionArticle($id)
        {
            
            $model = $this->findModel($id);
            
            return $this->render(
                'view',
                [
                    'article' => $model,
                ]
            );
            
        }
    
        protected function findModel($id)
        {
            /**
             * Some comment
             */
    
            $model = Article::find()
                            ->where(
                                [
                                    'id' => $id
                                ]
                            )
                            ->with("lang")
                            ->one();
            
            if ( $model !== NULL) {
                return $model;
            }
            else {
                throw new NotFoundHttpException('The requested page does not exist.');
            }
        }
        
    }