DefaultController.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 2016/12/15
  6. * Time: 下午2:05
  7. */
  8. namespace common\modules\book\frontend\controllers;
  9. use common\modules\book\models\Book;
  10. use common\modules\book\models\BookChapter;
  11. use yii\data\ActiveDataProvider;
  12. use yii\web\Controller;
  13. use Yii;
  14. use yii\web\NotFoundHttpException;
  15. class DefaultController extends Controller
  16. {
  17. public function actionIndex()
  18. {
  19. $dataProvider = new ActiveDataProvider(['query' => Book::find()]);
  20. return $this->render('index', [
  21. 'dataProvider' => $dataProvider
  22. ]);
  23. }
  24. public function actionView($id)
  25. {
  26. /**
  27. * @var Book $model
  28. */
  29. $model = Book::findOne($id);
  30. if ($model == null) {
  31. throw new NotFoundHttpException('书不存在');
  32. }
  33. $model->addView();
  34. return $this->render('view', [
  35. 'model' => $model
  36. ]);
  37. }
  38. public function actionChapter($id)
  39. {
  40. /**
  41. * @var BookChapter $model
  42. */
  43. $model = BookChapter::findOne($id);
  44. if ($model == null) {
  45. throw new NotFoundHttpException('章节不存在');
  46. }
  47. $model->book->addView();
  48. return $this->render('chapter', [
  49. 'model' => $model
  50. ]);
  51. }
  52. }