Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Last active November 2, 2017 05:00
Show Gist options
  • Save james2doyle/efc694e270efc0c9e5cf80f32d28f899 to your computer and use it in GitHub Desktop.
Save james2doyle/efc694e270efc0c9e5cf80f32d28f899 to your computer and use it in GitHub Desktop.
An OptionController for Laravel that allows users to find single or multiple config values
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class OptionController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index($key)
{
// explode will always return an array
$data = collect(explode(',', $key))
->map(function ($k) {
return [
"{$k}" => config('hitsource.' . $k, null),
];
})
->collapse()
->filter(function ($results) {
return !empty($results);
});
if ($data->count() < 1) {
return response()->json([
'status' => 'Not Found',
'message' => sprintf('Could not find options for %s', $key),
'data' => $data,
], 404);
}
return response()->json([
'status' => 'ok',
'message' => sprintf('Successfully found options for %s', $key),
'data' => $data,
], 200);
}
}
@james2doyle
Copy link
Author

james2doyle commented Nov 1, 2017

Route as Route::get('/options/{key}', 'OptionController@index');.

Usage:

/options/array_of_values

Returns:

...
data: {
    array_of_values: {...}
}
...

Usage:

/options/array_of_values,another_array_of_values

Returns:

...
data: {
    array_of_values: {...},
    another_array_of_values: {...}
}
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment