Category.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace addons\blog\controller;
  3. use addons\blog\model\Category as CategoryModel;
  4. use addons\blog\model\Comment;
  5. use addons\blog\model\Post;
  6. use think\Paginator;
  7. /**
  8. * 博客分类
  9. */
  10. class Category extends Base
  11. {
  12. public function index()
  13. {
  14. $diyname = $this->request->param('diyname');
  15. if ($diyname && !is_numeric($diyname)) {
  16. $category = CategoryModel::getByDiyname($diyname);
  17. } else {
  18. $id = $diyname ? $diyname : $this->request->param('id', '');
  19. $category = CategoryModel::get($id);
  20. }
  21. if (!$category || $category['status'] != 'normal') {
  22. $this->error("分类未找到");
  23. }
  24. $postlist = Post::where(['status' => 'normal'])
  25. ->where('category_id', $category['id'])
  26. ->with('category')
  27. ->order('weigh desc,id desc')
  28. ->paginate($this->view->config['listpagesize'], false, ['type' => '\\addons\\blog\\library\\Bootstrap']);
  29. $page = Paginator::getCurrentPage();
  30. $urls = $postlist->getUrlRange($page - 1, $page + 1);
  31. $prevurl = $page == 1 ? '' : array_shift($urls);
  32. $nexturl = $page == $postlist->lastPage() ? '' : array_pop($urls);
  33. $this->view->assign("postlist", $postlist);
  34. $this->view->assign('prevurl', $prevurl);
  35. $this->view->assign('nexturl', $nexturl);
  36. $this->view->assign('category', $category);
  37. $this->view->assign('title', $category['name']);
  38. $this->view->assign('keywords', $category['keywords']);
  39. $this->view->assign('description', $category['description']);
  40. if ($this->request->isAjax()) {
  41. return $this->view->fetch('/common/postlist');
  42. }
  43. return $this->view->fetch('/category');
  44. }
  45. }