Created
December 30, 2012 03:52
-
-
Save danharper/4410879 to your computer and use it in GitHub Desktop.
Implementing Eiffel in Laravel 4 is a piece of cake compared to Fuel.
This file contains hidden or 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 EiffelException extends Exception { | |
protected $msg = ''; | |
protected $http = 500; | |
public function __construct($message = null, $code = 0, Exception $previous = null) | |
{ | |
$this->msg = $message ?: $this->msg; | |
parent::__construct($this->msg, $code, $previous); | |
} | |
public function getHttpStatus() { return $this->http; } | |
} | |
class NotFound extends EiffelException { | |
protected $msg = 'Resource Not Found'; | |
protected $http = 404; | |
} | |
class SaveFailed extends EiffelException { | |
protected $msg = 'Save Failed; Please try again later'; | |
protected $http = 500; | |
} | |
class UploadFailed extends EiffelException { | |
protected $msg = 'File Upload Failed; Please try again later'; | |
protected $http = 500; | |
} | |
class NotImplemented extends EiffelException { | |
protected $msg = 'Not Implemented'; | |
protected $http = 501; | |
} | |
class Unauthorised extends EiffelException { | |
protected $msg = 'You must sign in to continue'; | |
protected $http = 401; | |
} | |
class Forbidden extends EiffelException { | |
protected $msg = 'You do not have permission to do that'; | |
protected $http = 403; | |
} | |
class Conflict extends EiffelException { | |
protected $msg = 'A Conflict Occurred'; | |
protected $http = 409; | |
} | |
class Unknown extends EiffelException { | |
protected $msg = 'An unknown error occurred; Please try again later'; | |
protected $http = 500; | |
} | |
class BadRequest extends EiffelException { | |
protected $msg = 'Bad request'; | |
protected $http = 400; | |
} | |
class Invalid extends EiffelException { | |
protected $msg = 'Validation Failed'; | |
protected $http = 400; | |
protected $errors; | |
public function __construct($errors = array(), $message = null, $code = 0, Exception $previous = null) | |
{ | |
$this->errors = $errors; | |
parent::__construct(); | |
} | |
public function getErrors() { return $this->errors; } |
This file contains hidden or 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 ApiJobsController extends BaseController | |
{ | |
public function index() | |
{ | |
throw new NotImplemented; | |
} | |
public function show($id) | |
{ | |
if ($job = Auth::user()->jobs->find($id)) | |
{ | |
return $job; // JSON encoded | |
} | |
throw new NotFound; | |
} | |
} |
This file contains hidden or 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 | |
App::error(function(Invalid $e, $code) | |
{ | |
return Response::json(array( | |
'message' => $e->getMessage(), | |
'errors' => $e->getErrors() | |
), $e->getHttpStatus()); | |
}); | |
App::error(function(Unauthorised $e, $code) | |
{ | |
return Response::json(array( | |
'message' => $e->getMessage(), | |
'redirect' => 'login' | |
), $e->getHttpStatus()); | |
}); | |
App::error(function(EiffelException $e, $code) | |
{ | |
return Response::json(array( | |
'message' => $e->getMessage() | |
), $e->getHttpStatus()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment