Last active
November 2, 2017 05:00
-
-
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
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 | |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Route as
Route::get('/options/{key}', 'OptionController@index');
.Usage:
Returns:
Usage:
Returns: