It is necessary to transfer the request parameters, there may be several of these parameters (depending on how many the user has selected), for example, you need to search for such and such a country, for such a date, for such a format, and so on, well, and finally output the result. The problem is that I can not pass the parameters from the front-end to the back-end .. I use AngularJS and slim framework.

Php code example

$app->get('/items', function (Request $request, Response $response) { // $file = 'log.txt'; $input = $request->getBody(); //file_put_contents($file,$input); $sql = 'SELECT DISTINCT items.name, items.link, items.owner, items.date_start, items.date_end, items.description, format_file.format_name FROM items, format_file WHERE items.id_format_file = format_file.id AND format_file.format_name = "' .$input['format'] . '"'; $rows = DB::fetchAll($sql); $response->getBody()->write(json_encode([ 'data' => $rows ], JSON_UNESCAPED_UNICODE)); return $response; }); 

and sample code js

 mainApp.controller('NewsViewController', function($scope, $http) { $scope.getPosts = function(){ var paramSearch = {}; paramSearch.country = $scope.country; paramSearch.format = $scope.format; var jsonParamSearch = []; jsonParamSearch[0] = 'country'; jsonParamSearch[1] = 'format'; var jsonParam = JSON.stringify(paramSearch, jsonParamSearch, "\t"); console.log(jsonParam); $http.get('http://localhost:8088/api/rest.php/items', jsonParam).success(function(){ console.log('done'); }); }; }); 

    1 answer 1

    To pass get parameters by request, they must be specified via params:

    $http.get('myUrl', {params: {country:"myCountry", format:"myFormat"}})...

    Try passing the parameters to the server like this:

     $scope.getPosts = function(){ var paramSearch = {}; paramSearch.country = "myCountry"; paramSearch.format = "myFormat"; $http.get('http://localhost:8088/api/rest.php/items', {params: paramSearch}) .success(function(){ console.log('done'); }); }; 

    In the browser, press f12 -> network and see what data is being transmitted.

    • slim cannot read, it is $ input = $ request-> getBody (); returns the void, the transfer occurs in the console: /api/rest.php/items/?country=myCountry&format=myFormat - Maxim Kokhansky
    • one
      I don’t know php myself, but judging by the documentation, try $input = $request->getQueryParams(); should earn. - MrFylypenko