-
-
Save anlutro/26d630d0b573e69a7ca1 to your computer and use it in GitHub Desktop.
Abstracting logic from controllers using managers
This file contains 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 | |
class BadController | |
{ | |
public function update($brand) | |
{ | |
$rules = array( | |
'title' => 'required|min:3' | |
); | |
$validator = Validator::make(Input::all(), $rules); | |
if ($validator->passes()) { | |
$brand->title = Input::get('title'); | |
$brand->meta_title = Input::get('meta-title'); | |
$brand->meta_description = Input::get('meta-description'); | |
$brand->meta_keywords = Input::get('meta-keywords'); | |
$brand->logo = Input::file('logo'); | |
$brand->checked = Input::get('checked'); | |
$brand->body = Input::get('body'); | |
if ($brand->save()) { | |
$areas = Input::get('areas'); | |
if (is_array($areas)) { | |
$brand->areas()->sync($areas); | |
} | |
return Redirect::to('admin/brands') | |
->with('success', Lang::get('admin/brands/messages.update.success')); | |
} | |
return Redirect::to('admin/brands/' . $brand->id . '/edit') | |
->with('error', Lang::get('admin/brands/messages.update.error')); | |
} | |
return Redirect::to('admin/brands/' . $brand->id . '/edit') | |
->withInput()->withErrors($validator); | |
} | |
} |
This file contains 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 | |
class GoodController | |
{ | |
protected $brandManager; | |
public function __construct(BrandManager $brandManager) | |
{ | |
$this->brandManager = $brandManager; | |
} | |
public function update(BrandModel $brand) | |
{ | |
if (!$this->brandManager->update($brand, Input::all())) { | |
return Redirect::to('admin/brands/' . $brand->id . '/edit') | |
->withInput()->withErrors($this->brandManager->getErrors()); | |
} | |
return Redirect::to('admin/brands') | |
->with('success', Lang::get('admin/brands/messages.update.success')); | |
} | |
} | |
class BrandManager | |
{ | |
public function update(BrandModel $brand, array $data) | |
{ | |
$rules = array( | |
'title' => 'required|min:3' | |
); | |
$validator = Validator::make($data, $rules); | |
if (!$validator->passes()) { | |
$this->errors = $validator; | |
return false; | |
} | |
$brand->title = array_get($data, 'title'); | |
$brand->meta_title = array_get($data, 'meta-title'); | |
$brand->meta_description = array_get($data, 'meta-description'); | |
$brand->meta_keywords = array_get($data, 'meta-keywords'); | |
$brand->logo = array_get($data, 'logo'); | |
$brand->checked = array_get($data, 'checked'); | |
$brand->body = array_get($data, 'body'); | |
$brand->save(); | |
if (isset($data['areas']) && is_array($data['areas'])) { | |
$brand->areas()->sync($data['areas']); | |
} | |
return true; | |
} | |
public function getErrors() | |
{ | |
return $this->errors; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment