-
-
Save dariodiaz/7c5acef48369bdf3013e to your computer and use it in GitHub Desktop.
php: Laravel controller cleanup
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 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'); | |
} | |
} |
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 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(); | |
} |
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 | |
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