Last active
August 29, 2015 14:14
-
-
Save coreymcmahon/d09f465ac90cf046498f to your computer and use it in GitHub Desktop.
ExampleController.php - A Pattern for Reusable Resource Controllers in Laravel 4.2 http://slashnode.com/reusable-resource-controllers/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Controllers; | |
class ExampleResourceController | |
{ | |
protected $repo; | |
protected $model; | |
protected $validator; | |
public function __construct(Repository $repo, Model $model, Validator $validator) | |
{ | |
$this->repo = $repo; | |
$this->model = $model; | |
$this->validator = $validator; | |
} | |
public function index() | |
{ | |
return view('example.index', [ | |
'items' => $this->repo->paginate()); | |
]); | |
} | |
public function edit($id) | |
{ | |
return view('example.edit', [ | |
'item' => $this->repo->find($id) | |
]); | |
} | |
public function create() | |
{ | |
return view('example.create', [ | |
'item' => new $this->model() | |
]); | |
} | |
public function store() | |
{ | |
if ($this->validator->with(Input::all())->fails()) | |
return redirect('example.create'); // with errors | |
$this->repo->create(Input::all()); | |
return redirect('example.index'); // with success message | |
} | |
public function update($id) | |
{ | |
if ($this->validator->with(Input::all())->fails()) | |
return redirect('example.edit', $id); // with errors | |
$this->repo->update($this->repo->find($id), Input::all()); | |
return redirect('exmaple.index'); // with success message | |
} | |
public function destroy($id) | |
{ | |
$this->repo->delete($id); | |
return redirect('example.index'); // with success message | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment