Mam taki kod (Laravel 7):
Controller:
class PageController extends Controller { protected $repository; public function __construct(PageRepository $repository) { $this->repository = $repository; } public function index() { return view('cms.page.list'); } public function create() { return view('cms.page.view'); } public function store(PageCreateRequest $request) { $data = [ 'title' => $request->input('title'), 'description' => $request->input('description')$request->input('title'),
'keywords' => $request->input('keywords')$request->input('title'),
'content' => $request->input('content'), 'enable' => $request->input('enable')0,
]; $this->repository->create($data); return redirect()->route('page.index')->with('success', 'Nowy rekord zapisany poprawnie'); } public function edit(int $id) { return view('cms.page.view', ['page' => $this->repository->getModel($id), 'fileCount' => 5, 'fileFolder' => 'DZ_TEXT_PAGE', 'fileId' => $id]); } public function update(PageUpdateRequest $request, int $id) { $data = [ 'title' => $request->input('title'), 'description' => $request->input('description'), 'keywords' => $request->input('keywords'), 'content' => $request->input('content'), 'enable' => $request->input('enable')0,
]; $this->repository->update($data, $id); return redirect()->route('page.index')->with('success', 'Zmiany zapisane pomyślnie'); } public function destroy(Request $request, int $id, DropZoneService $dropZoneService) { $fileList = $dropZoneService->getFilesList($id, 'products'); foreach ($fileList as $key => $value) { $dropZoneService->removeDropZoneFile($id, 'products', $value->id); } $this->repository->delete($id); return redirect()->route('page.index')->with('success', 'Rekord usunięty poprawnie'); } public function dataTable(Request $request) { if ($request->ajax()) { return Datatables::of($this->repository->getAll()) ->addIndexColumn() ->editColumn('enable', function ($row) { if ($row->enable == 1) return '<span class="btn btn-block btn-info btn-sm text-white d-sm-inline mr-md-1 mr-sm-1 py-sm-2"><i class="far fa-smile-beam pr-lg-1 d-inline-block d-lg-inline"></i><span class="d-none d-xl-inline">Aktywny</span> </span>'; else return '<span class="btn btn-block btn-secondary btn-sm text-white d-sm-inline mr-md-1 mr-sm-1 py-sm-2"> <i class="far fa-frown pr-lg-1 d-inline-block d-lg-inline"></i><span class="d-none d-xl-inline">Nieaktywny</span> </span>'; }) ->editColumn('title', function ($row) { return Str::limit($row->title, 80, '...'); }) ->editColumn('title', function ($row) { return '<a href="' . route('page.edit', ['id' => $row->id]) . '">' . $row->title . '</a>'; }) ->addColumn('action', function ($row) { $btn = '<a href="' . route('page.edit', ['id' => $row->id]) . '" class="btn btn-block btn-warning btn-sm text-white d-sm-inline mr-md-1 mr-sm-1 py-sm-2"><i class="far fa-edit pr-lg-1 d-inline-block d-lg-inline"></i></a> '; $btn .= '<a href="' . route('page.destroy', ['id' => $row->id]) . '" class="btn btn-block btn-danger btn-sm text-white d-sm-inline py-sm-2"><i class="far fa-trash-alt pr-lg-1 d-inline-block d-lg-inline"></i></a> '; return $btn; }) ->rawColumns(['title', 'action', 'enable']) ->make(true); } }
Page Repository:
class PageRepository { public function __construct(Page $model) { $this->model = $model; } /** * @param string $slug * @return mixed */ public function getTextPageFromSlug(string $slug) { return $this->model->active()->where('slug', $slug)->first(); } public function getAll() { return $this->model->all(); } { $this->model->create($data); } { $record = $this->model->findOrFail($id); $record->title = $data['title']; $record->description = $data['description']; $record->keywords = $data['keywords']; $record->content = $data['content']; $record->enable = $data['enable']0;
$record->save(); } public function delete(int $id) { $record = $this->model->findOrFail($id); $record->delete(); } public function getModel($id) { return $this->model->findOrFail($id); } }
(Controller + repositorium). Nie korzystam w projekcie z Repository Pattern: https://bosnadev.com/2015/03/07/using-repos...aign=prettus-l5
Wydzieliłem sobie tylko funkcje związane z bazami danych do osobnego Pliku (Page Repository). Czy takie podejście jest dobre (dobre praktyki programowania)?
Czy lepiej te funkcje z PageRepository przenieść do Modelu?
Czytałem troszkę w internecie, i z tego co widzę to ludzie zalecają PageRepository - a w momencie gdy controller robi się zbyt "duży" to dodatkowo serwisy.
Czy mógłbym prosić o opinie?
