ArticleController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Auth;
  4. use Request;
  5. use App\Models\Article;
  6. use App\Http\Requests\ArticleRequest;
  7. class ArticleController extends BaseController
  8. {
  9. /* 视图文件相对路径 */
  10. protected $view = 'admin.article.list';
  11. protected $view_addupd = 'admin.article.addupd';
  12. protected $menu_name = 'article';
  13. public function __construct()
  14. {
  15. parent::__construct($this->menu_name);
  16. }
  17. // 获取所属菜单选项
  18. public function getBelongMenus()
  19. {
  20. $cor_sort1 = $this->menu_home->where('ename', 'cor')
  21. ->first()
  22. ->sort1;
  23. $info_sort1 = $this->menu_home->where('ename', 'info')
  24. ->first()
  25. ->sort1;
  26. $menus_sort1 = [$cor_sort1, $info_sort1];
  27. return $this->getSomeSecondMenus($menus_sort1);
  28. }
  29. /* 文章管理 */
  30. // 页面展示
  31. public function index()
  32. {
  33. $articles = Article::where([])
  34. ->orderBy('created_at', 'desc')
  35. ->paginate(10);
  36. $view_para = array_add($this->view_para, 'articles', $articles);
  37. $view_para = array_add($view_para, 'menu_home', $this->menu_home);
  38. return view($this->view, $view_para);
  39. }
  40. /* 添加 */
  41. // 页面展示
  42. public function addView()
  43. {
  44. $view_para = array_add($this->view_para, 'menus', $this->getBelongMenus());
  45. return view($this->view_addupd, $view_para);
  46. }
  47. // 表单处理
  48. public function addPost(ArticleRequest $request)
  49. {
  50. $cover = Request::file('cover');
  51. if (!$cover) {
  52. return redirect()->back()->with('errors_image', '封面不可为空')->withInput();
  53. }
  54. $errors_cover = $this->imageValidator($cover);
  55. if ($errors_cover) {
  56. return redirect()->back()->with('errors_image', $errors_cover)->withInput();
  57. }
  58. $path = $this->imageUpload($this->image_name, $cover, $this->image_path);
  59. $data = Request::except('cover');
  60. $data['cover'] = $path;
  61. $data['belong'] = implode(',', Request::input('belong'));
  62. Article::create($data);
  63. return redirect()->route($this->menu_name)->with('success', '添加成功');
  64. }
  65. /* 编辑 */
  66. // 页面展示
  67. public function updView($id)
  68. {
  69. $article = Article::find($id);
  70. if (!$article) {
  71. return redirect()->route($this->menu_name)->with('error', '找不到该文章');
  72. }
  73. $view_para = array_add($this->view_para, 'article', $article);
  74. $view_para = array_add($view_para, 'menus', $this->getBelongMenus());
  75. return view($this->view_addupd, $view_para);
  76. }
  77. // 表单处理
  78. public function updPost($id, ArticleRequest $request)
  79. {
  80. $article = Article::find($id);
  81. if (!$article) {
  82. return redirect()->route($this->menu_name)->with('error', '找不到该文章');
  83. }
  84. $cover = Request::file('cover');
  85. if ($cover) {
  86. $errors_cover = $this->imageValidator($cover);
  87. if ($errors_cover) {
  88. return redirect()->back()->with('errors_image', $errors_cover)->withInput();
  89. }
  90. $path = $this->imageUpload($this->image_name, $cover, $this->image_path);
  91. $article->cover = $path;
  92. }
  93. $data = Request::except(['_token', 'path']);
  94. $data['belong'] = implode(',', Request::input('belong'));
  95. Article::where('id', $id)
  96. ->update($data);
  97. return redirect()->route($this->menu_name)->with('success', '编辑成功');
  98. }
  99. /* 删除 */
  100. public function del($id)
  101. {
  102. Article::destroy($id);
  103. return redirect()->route($this->menu_name)->with('success', '删除成功');
  104. }
  105. }