Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@danharper
Created December 30, 2012 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danharper/4410879 to your computer and use it in GitHub Desktop.
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.
<?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; }
<?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;
}
}
<?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