Last active
August 29, 2015 14:05
-
-
Save Sigmus/63fc96f969173d409109 to your computer and use it in GitHub Desktop.
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 Krudder { | |
public static function add($route, $modelName) | |
{ | |
App::bind("Krudder$route", function() use($route, $modelName) | |
{ | |
return new KrudderController($route, $modelName); | |
}); | |
Route::resource($route, "Krudder$route"); | |
} | |
} |
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 KrudderController extends Controller { | |
public function __construct($route, $modelName) | |
{ | |
$this->route = $route; | |
$this->modelName = $modelName; | |
} | |
public function index() | |
{ | |
$model = new $this->modelName; | |
if (method_exists($model, 'defaultQuery')) | |
{ | |
$query = $model->defaultQuery(); | |
} | |
else | |
{ | |
$query = $model->orderBy('id'); | |
} | |
return View::make('admin.'.$this->route.'.list') | |
->with('collection', $query->get()) | |
->with('route', $this->route); | |
} | |
protected function formView($id = 0) | |
{ | |
if ($id > 0) | |
{ | |
$action = "admin/".$this->route."/$id"; | |
$method = 'PUT'; | |
$model = with(new $this->modelName)->find($id); | |
} | |
else | |
{ | |
$action = "admin/".$this->route; | |
$method = 'POST'; | |
$model = new $this->modelName; | |
} | |
return View::make('admin.'.$this->route.'.form') | |
->with(compact('action', 'method', 'model')) | |
->with('route', $this->route); | |
} | |
protected function listView() | |
{ | |
return Redirect::to('/admin/'.$this->route); | |
} | |
protected function validate($input) | |
{ | |
$model = new $this->modelName; | |
$validator = Validator::make($input, $model::$rules); | |
$hasFailed = $validator->fails(); | |
if ($hasFailed) { | |
Former::withErrors($validator); | |
} | |
return ! $hasFailed; | |
} | |
public function show($id) | |
{ | |
$model = with(new $this->modelName)->find($id); | |
Former::populate($model); | |
return $this->formView($id); | |
} | |
public function create() | |
{ | |
return $this->formView(); | |
} | |
public function store() | |
{ | |
$input = Input::all(); | |
if ($this->validate($input)) | |
{ | |
$model = new $this->modelName; | |
$model->fill($input); | |
if (method_exists($model, 'processUploads')) | |
{ | |
$model->processUploads(); | |
} | |
$model->save(); | |
return $this->listView(); | |
} | |
return $this->formView(); | |
} | |
public function update($id) | |
{ | |
$input = Input::all(); | |
if ($this->validate($input)) | |
{ | |
$model = with(new $this->modelName)->find($id); | |
$model->fill($input); | |
if (method_exists($model, 'processUploads')) | |
{ | |
$model->processUploads(); | |
} | |
$model->save(); | |
return $this->listView(); | |
} | |
return $this->formView($id); | |
} | |
} |
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 | |
trait Uploader { | |
public function processUploads() | |
{ | |
if ( ! isset($this::$files)) | |
{ | |
return; | |
} | |
foreach ($this::$files as $fieldName) | |
{ | |
$this->processUpload($fieldName); | |
} | |
} | |
public function processUpload($fieldName) | |
{ | |
if ( ! Input::hasFile($fieldName)) return; | |
$file = Input::file($fieldName); | |
$name = time().'.'.$file->getClientOriginalExtension(); | |
$file->move('uploads', $name); | |
$this->$fieldName = $name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment