Skip to content

Instantly share code, notes, and snippets.

@lucadegasperi
Created April 19, 2014 08:51
Show Gist options
  • Save lucadegasperi/11078350 to your computer and use it in GitHub Desktop.
Save lucadegasperi/11078350 to your computer and use it in GitHub Desktop.
Controller Cleanup + Interface
<?php
class ProjectController extends BaseController implements ProjectCreatorDelegate
{
public function store()
{
$creator = new ProjectCreator($this);
return $creator->create(Input::all());
}
public function projectCreationFailed($errors)
{
return Redirect::back()->withInput()->withErrors($errors);
}
public function projectCreationSucceded()
{
return Redirect::route('projects.index');
}
}
<?php
class ProjectCreator
{
protected $delegate;
public function __construct(ProjectCreatorDelegate $delegate)
{
$this->delegate = $delegate;
}
public function create($input)
{
$validation = Validator::make($input, ['name' => 'min:8']);
if ( $validation->fails() ) {
return $this->delegate->projectCreationFailed($validation->messages());
}
// do db stuff
return $this->delegate->projectCreationSucceeded();
}
<?php
interface ProjectCreatorDelegate
{
public function projectCreationFailed($errors);
public function projectCreationSucceeded();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment