1

I want to know the best practices for passing array in url and how can i define my routes for accepting that array in laravel 5 and pass it to the controller. Any Suggestions??

Let us take an example

URL - http://localhost/new/public/api/getaudit/

In this url, I want to pass array. Like: array('car','bike') Thanks

FaNtAMode
  • 159
  • 2
  • 9
  • 1
    Have you tried anything, yet? – swidmann Nov 20 '15 at 14:17
  • use a standard get request with query parameters: `http://localhost/new/public/api/getaudit/?array=car&array=bike` To access, `$_GET['array']` or whatever wrapper laravel uses, something like `Request::get('array')` – Steve Nov 20 '15 at 14:21
  • 1
    @Steve: this `array=car&array=bike` will override one value with the other, so in `$_GET['array']`is just the value `bike`, you need `array[]=car&array[]=bike` – swidmann Nov 20 '15 at 14:25
  • 2
    @swidmann oh yes, i forget that php does that (its a perfectly valid query string), amended: `http://localhost/new/public/api/getaudit/?array[]=car&array[]=bike` – Steve Nov 20 '15 at 14:28
  • 1
    In our project we solve exactly this problem (getting audit) with a POST and send an JSON-Array with IDs as body. It was our preferred solution instead of doing one get-Request per audit or using URL-params. – Hecke29 Nov 20 '15 at 14:28
  • @steve if my array elements are at higher no of side.. Does it look suitable ? – FaNtAMode Nov 20 '15 at 14:30
  • @FaNtAMode Should be fine: http://stackoverflow.com/questions/812925/what-is-the-maximum-possible-length-of-a-query-string – Steve Nov 20 '15 at 14:32
  • 1
    you could also jusy use comma seperated string and explode it yourself, to save a few chars: `/?array=car,bike` `$array = explode(',', $_GET['array'];` – Steve Nov 20 '15 at 14:33
  • @steve Thanks steve exactly what i was thinking :) – FaNtAMode Nov 20 '15 at 14:37

0 Answers0