Created
January 27, 2015 10:35
-
-
Save coreymcmahon/a6e613e9d562bb8a5c60 to your computer and use it in GitHub Desktop.
BaseResourceController.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; | |
abstract class BaseResourceController extends BaseController | |
{ | |
protected $repo; | |
protected $model; | |
protected $validator; | |
public function index() | |
{ | |
return \View::make($this->getFullRouteName('index')) | |
->with('items', $this->repo->paginate()); | |
} | |
public function edit($id) | |
{ | |
$data = array_merge($this->formData($id), [ | |
'item' => $this->repo->find($id) | |
]); | |
return \View::make($this->getFullRouteName('edit'), $data); | |
} | |
public function create() | |
{ | |
$data = array_merge($this->formData(), [ | |
'item' => new $this->model() | |
]); | |
return \View::make($this->getFullRouteName('create'), $data); | |
} | |
public function store() | |
{ | |
if ($this->validator->with($this->data())->fails()) { | |
return $this->redirectToSubRoute() | |
->withErrors($this->validator->errors()); | |
} | |
$this->repo->create($this->data()); | |
return $this->redirectToSubRoute('index') | |
->with('success_message', 'Saved '.$this->resourceName()); | |
} | |
public function update($id) | |
{ | |
if ($this->validator->with($this->data())->fails()) { | |
return \Redirect::back() | |
->withErrors($this->validator->errors()); | |
} | |
$this->repo->update($this->repo->find($id), $this->data()); | |
return $this->redirectToSubRoute('index') | |
->with('success_message', 'Updated '.$this->resourceName()); | |
} | |
public function destroy() | |
{ | |
return $this->redirectToSubRoute('index') | |
->with('error_message', 'Function not supported'); | |
} | |
/** | |
* Returns a structured route name based on the user and resource types. | |
* | |
* @param $subroute | |
* @return \Illuminate\Routing\Redirector | |
*/ | |
protected function redirectToSubRoute($subroute = null) | |
{ | |
if (is_null($subroute)) { | |
return \Redirect::back(); | |
} | |
return \Redirect::route($this->getFullRouteName($subroute)); | |
} | |
/** | |
* @param $subroute | |
* @return string | |
*/ | |
protected function getFullRouteName($subroute) | |
{ | |
$role = $this->userRole(); | |
$resource_name = $this->resourceName(); | |
return "{$role}.{$resource_name}.{$subroute}"; | |
} | |
/** | |
* Inject any values needed by the form here | |
* | |
* @param $entity_id | |
* @return array | |
*/ | |
protected function formData($entity_id = null) | |
{ | |
return []; | |
} | |
/** | |
* @return string | |
*/ | |
abstract protected function userRole(); | |
/** | |
* @return string | |
*/ | |
abstract protected function resourceName(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment