controller.tpl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * {{desc}}
  4. * @author system
  5. * @version 1.0
  6. * @date {{date}}
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\{{sortPath}};
  10. use App\Http\Controllers\Admin\Controller;
  11. use Illuminate\Http\Request;
  12. use App\Repositories\Base\Criteria\OrderBy;
  13. use {{CriteriaMultiWhereUse}};
  14. use {{repositoriesUse}};
  15. class {{controlName}} extends Controller
  16. {
  17. private $repository;
  18. public function __construct({{repositoriesName}} $repository) {
  19. if(!$this->repository) $this->repository = $repository;
  20. }
  21. /**
  22. * 列表页
  23. */
  24. function index(Request $request) {
  25. $search['keyword'] = $request->input('keyword');
  26. $query = $this->repository->pushCriteria(new MultiWhere($search));
  27. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  28. $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
  29. }else{
  30. $query = $query->pushCriteria(new OrderBy('updated_at','DESC'));
  31. }
  32. $list = $query->paginate(16);
  33. return view('{{viewSortPath}}.index',compact('list'));
  34. }
  35. /**
  36. * 添加
  37. */
  38. public function create(Request $request)
  39. {
  40. if($request->method() == 'POST') {
  41. return $this->_createSave();
  42. }
  43. return view('{{viewSortPath}}.edit');
  44. }
  45. /**
  46. * 保存修改
  47. */
  48. private function _createSave(){
  49. $data = (array) request('data');
  50. $id = $this->repository->create($data);
  51. if($id) {
  52. $url[] = array('url'=>U( '{{path}}/index'),'title'=>'返回列表');
  53. $url[] = array('url'=>U( '{{path}}/create'),'title'=>'继续添加');
  54. $this->showMessage('添加成功',$url);
  55. }else{
  56. $url[] = array('url'=>U( '{{path}}/index'),'title'=>'返回列表');
  57. return $this->showWarning('添加失败',$url);
  58. }
  59. }
  60. /**
  61. * 修改
  62. */
  63. public function update(Request $request) {
  64. if($request->method() == 'POST') {
  65. return $this->_updateSave();
  66. }
  67. $data = $this->repository->find($request->get('id'));
  68. return view('{{viewSortPath}}.edit',compact('data'));
  69. }
  70. /**
  71. * 保存修改
  72. */
  73. private function _updateSave() {
  74. $data = (array) request('data');
  75. $ok = $this->repository->update(request('id'),$data);
  76. if($ok) {
  77. $url[] = array('url'=>U( '{{path}}/index'),'title'=>'返回列表');
  78. return $this->showMessage('操作成功',urldecode(request('_referer')));
  79. }else{
  80. $url[] = array('url'=>U( '{{path}}/index'),'title'=>'返回列表');
  81. return $this->showWarning('操作失败',$url);
  82. }
  83. }
  84. public function view(Request $request) {
  85. $data = $this->repository->find(request('id'));
  86. return view('{{viewSortPath}}.view',compact('data'));
  87. }
  88. /**
  89. * 删除
  90. */
  91. public function destroy(Request $request) {
  92. $bool = $this->repository->destroy($request->get('id'));
  93. if($bool) {
  94. return $this->showMessage('操作成功');
  95. }else{
  96. return $this->showWarning("操作失败");
  97. }
  98. }
  99. }