@@ -122,6 +122,8 @@ return array(
|
||||
'Illuminate\View\ViewServiceProvider',
|
||||
'Illuminate\Workbench\WorkbenchServiceProvider',
|
||||
|
||||
'Dingo\Api\ApiServiceProvider',
|
||||
|
||||
),
|
||||
|
||||
/*
|
||||
@@ -190,6 +192,8 @@ return array(
|
||||
'Validator' => 'Illuminate\Support\Facades\Validator',
|
||||
'View' => 'Illuminate\Support\Facades\View',
|
||||
|
||||
'API' => 'Dingo\Api\Facades\API'
|
||||
|
||||
),
|
||||
|
||||
);
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| API Vendor
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Your vendor is used in the "Accept" request header and will be used by
|
||||
| the consumers of your API. Typically this will be the name of your
|
||||
| application or website.
|
||||
|
|
||||
*/
|
||||
|
||||
'vendor' => 'cachet',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Default API Version
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| When a request is made to the API and no version is specified then it
|
||||
| will default to the version specified here.
|
||||
|
|
||||
*/
|
||||
|
||||
'version' => 'v1',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Default API Prefix
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| A default prefix to use for your API routes so you don't have to
|
||||
| specify it for each group.
|
||||
|
|
||||
*/
|
||||
|
||||
'prefix' => null,
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Default API Domain
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| A default domain to use for your API routes so you don't have to
|
||||
| specify it for each group.
|
||||
|
|
||||
*/
|
||||
|
||||
'domain' => null,
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Conditional Requests
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Globally enable conditional requests so that an ETag header is added to
|
||||
| any successful response. Subsequent requests will perform a check and
|
||||
| will return a 304 Not Modified. This can also be enabled or disabled
|
||||
| on certain groups or routes.
|
||||
|
|
||||
*/
|
||||
|
||||
'conditional_request' => true,
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Authentication Providers
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The authentication providers that should be used when attempting to
|
||||
| authenticate an incoming API request.
|
||||
|
|
||||
*/
|
||||
|
||||
'auth' => [
|
||||
'basic' => function ($app) {
|
||||
return new Dingo\Api\Auth\BasicProvider($app['auth']);
|
||||
}
|
||||
],
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Rate Limiting
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Consumers of your API can be limited to the amount of requests they can
|
||||
| make. You can configure the limit based on whether the consumer is
|
||||
| authenticated or unauthenticated.
|
||||
|
|
||||
| The "limit" is the number of requests the consumer can make within a
|
||||
| certain amount time which is defined by "reset" in minutes.
|
||||
|
|
||||
| By default rate limiting is disabled.
|
||||
|
|
||||
*/
|
||||
|
||||
'rate_limiting' => [
|
||||
|
||||
'authenticated' => [
|
||||
'limit' => 0,
|
||||
'reset' => 60
|
||||
],
|
||||
|
||||
'unauthenticated' => [
|
||||
'limit' => 0,
|
||||
'reset' => 60
|
||||
],
|
||||
|
||||
'exceeded' => 'API rate limit has been exceeded.'
|
||||
|
||||
],
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Response Transformer
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Responses can be transformed so that they are easier to format. By
|
||||
| default a Fractal transformer will be used to transform any
|
||||
| responses prior to formatting. You can easily replace
|
||||
| this with your own transformer.
|
||||
|
|
||||
*/
|
||||
|
||||
'transformer' => function ($app) {
|
||||
$fractal = new League\Fractal\Manager;
|
||||
|
||||
return new Dingo\Api\Transformer\FractalTransformer($fractal);
|
||||
},
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Response Formats
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Responses can be returned in multiple formats by registering different
|
||||
| response formatters. You can also customize an existing response
|
||||
| formatter.
|
||||
|
|
||||
*/
|
||||
|
||||
'default_format' => 'json',
|
||||
|
||||
'formats' => [
|
||||
|
||||
'json' => new Dingo\Api\Http\ResponseFormat\JsonResponseFormat
|
||||
|
||||
]
|
||||
|
||||
];
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
class ApiController extends Dingo\Api\Routing\Controller{
|
||||
|
||||
public function getComponents() {
|
||||
return Component::all();
|
||||
}
|
||||
|
||||
public function getComponent($id) {
|
||||
$component = Component::find($id);
|
||||
if ($component) {
|
||||
return $component;
|
||||
} else {
|
||||
App::abort(404, 'Component not found');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
class Component extends Eloquent {
|
||||
class Component extends Eloquent implements Dingo\Api\Transformer\TransformableInterface {
|
||||
/**
|
||||
* Looks up the human readable version of the status.
|
||||
* @return string
|
||||
@@ -26,4 +26,14 @@
|
||||
case 4: return 'text-danger';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the transformer instance.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTransformer()
|
||||
{
|
||||
return new ComponentTransformer();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
Route::api(['version' => 'v1', 'prefix' => 'api'], function()
|
||||
{
|
||||
|
||||
Route::get('components', 'ApiController@getComponents');
|
||||
Route::get('components/{id}', 'ApiController@getComponent');
|
||||
|
||||
});
|
||||
@@ -16,6 +16,7 @@ ClassLoader::addDirectories(array(
|
||||
app_path().'/commands',
|
||||
app_path().'/controllers',
|
||||
app_path().'/models',
|
||||
app_path().'/transformers',
|
||||
app_path().'/database/seeds',
|
||||
app_path().'/filters',
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
class ComponentTransformer extends League\Fractal\TransformerAbstract {
|
||||
|
||||
public function transform(Component $component)
|
||||
{
|
||||
return [
|
||||
'id' => (int) $component->id,
|
||||
'name' => $component->name,
|
||||
'description' => $component->description,
|
||||
'status_id' => (int) $component->status,
|
||||
'status' => $component->getHumanStatusAttribute(),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user