Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dariodiaz/7c5acef48369bdf3013e to your computer and use it in GitHub Desktop.
Save dariodiaz/7c5acef48369bdf3013e to your computer and use it in GitHub Desktop.
php: Laravel controller cleanup
<?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