Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MyirLik's avatar

Convert actually code to resfull api

Hi, I have the initial code and I would like to simplify it in some restful api with parameter in the route. E.g: I would like the controller to validate what I take from the route, send it to a factory and return the csv or what I have in the database. Forget the original code

public function convert($type) {
        switch($type) {
            case 'csv':
                $csv = Reader::createFromPath(');
                $csv = $csv->setOffset(1);
                $json = json_encode($csv, JSON_PRETTY_PRINT);
                return response($json);
            break;
            case 'db':
                $data = DB::table('transactions')->get();
                $json = json_encode($data, JSON_PRETTY_PRINT);
                return response($json);
            break;
            default:
                $error = array(
                    'status' => 'error',
                    'message' => 'Error'
                );
                return response()->json($error, 500);
        }
    }
Route::get('convert/{type}', 'App\Http\Controllers\ConvertController@convert');
And I would like for example to give a parameter of the route and to receive what I have and to check only in the controller and to take the rest from the factory
For exemple
Route::get('convert/source=?{type})'

0 likes
0 replies

Please or to participate in this conversation.