Skip to content

Instantly share code, notes, and snippets.

@progsmile
Last active May 10, 2017 19:33
Show Gist options
  • Save progsmile/5c2ec909fcba8ab978f67e8fb2a488fe to your computer and use it in GitHub Desktop.
Save progsmile/5c2ec909fcba8ab978f67e8fb2a488fe to your computer and use it in GitHub Desktop.
Crud Controller
<?php
namespace Core\Http\Controllers;
use App;
use View;
use Redirect;
use Illuminate\Http\Request;
use Core\Contracts\AdminRepositoryContract;
abstract class AdminCrudController
{
/** @var AdminRepositoryContract */
protected $repository;
protected $model;
protected $viewModel;
protected $entityName;
protected $routePrefix;
protected $storeRequest;
protected $updateRequest;
public function __construct()
{
$this->viewModel = App::make($this->viewModel);
$this->repository = App::make($this->repository);
View::share('routePrefix', $this->routePrefix);
View::share('entityName', $this->entityName);
}
public function index(Request $request)
{
return View::make($this->getIndexView(), [
'grid' => $this->viewModel->getGrid($request),
'routePrefix' => $this->routePrefix,
'entityName' => ucfirst($this->entityName)
]);
}
public function create()
{
return $this->showEditForm(new $this->model);
}
public function store()
{
$request = App::make($this->storeRequest ?? Request::class);
$entity = $this->repository->createByAdmin($request);
return Redirect::route($this->routePrefix . '.edit', [$entity->id]);
}
public function edit($id)
{
return $this->showEditForm($this->repository->findFirstById($id));
}
public function update($id)
{
$request = App::make($this->updateRequest ?? Request::class);
$entity = $this->repository->updateByAdmin($request,
$this->repository->findFirstById($id)
);
return Redirect::route($this->routePrefix . '.edit', [$entity->id]);
}
public function destroy($id)
{
$this->repository->deleteEntity($id);
}
protected function showEditForm($entity)
{
return View::make($this->getEditView(), [
'entity' => $entity,
'formFields' => $this->viewModel->getFormFields($entity),
]);
}
protected function getIndexView()
{
return 'admin.components.genericView.index';
}
protected function getEditView()
{
return 'admin.components.genericView.edit';
}
}
<?php
namespace Modules\ProductManage\Admin\Controllers;
use Modules\ProductManage\Common\Models\Product;
use Core\Http\Controllers\AdminCrudController;
use Modules\ProductManage\Common\ViewModels\ProductView;
use Modules\ProductManage\Common\Repositories\ProductRepository;
class ProductController extends AdminCrudController
{
protected $repository = ProductRepository::class;
protected $model = Product::class;
protected $viewModel = ProductView::class;
protected $entityName = 'product';
protected $routePrefix = 'admin.productmanage.products';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment