NewsController.php 893 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace app\controllers;
  3. use app\models\News;
  4. use yii\web\Controller;
  5. use yii\web\NotFoundHttpException;
  6. class NewsController extends Controller
  7. {
  8. /**
  9. * @param int $id
  10. * @return string
  11. */
  12. public function actionView(int $id):string {
  13. $model = $this->findModel($id);
  14. if(!$model instanceof News) throw new NotFoundHttpException("Новость не найдена");
  15. return $this->render('view',['model'=>$model]);
  16. }
  17. public function actionStoryslugView($storyslug):string
  18. {
  19. $model = News::find()->andWhere(['alias'=>$storyslug])->one();
  20. if(!$model instanceof News) throw new NotFoundHttpException("Новость не найдена");
  21. return $this->render('view',['model'=>$model]);
  22. }
  23. private function findModel($id)
  24. {
  25. return News::find()->andWhere(['id'=>$id])->one();
  26. }
  27. }