Skip to content

Instantly share code, notes, and snippets.

@moradi-morteza
Last active December 22, 2019 17:54
Show Gist options
  • Save moradi-morteza/a2258f9595dd0d6cf2a69b0777e971f3 to your computer and use it in GitHub Desktop.
Save moradi-morteza/a2258f9595dd0d6cf2a69b0777e971f3 to your computer and use it in GitHub Desktop.
[response] #LaravelT
// response should be an object(should be have toString method) or string or array , it can not be a boolean
// if you return an array laravel by default convert it to json
retrun ["name"=>"ali","age"=>25];
// laravel prefer response
return response('hello',201,[
'SAMPLE_HEADER'=>'VALUE'
]);
return response('hello',201)->header('second_header','salam');
return response('hello',201)->withHeaders(['Sampel_Header'=>'hello','second_header'=>'salam']);
return response('hello',201)->cookie('mycookie','salam'); // cookie by default encrypt by middleware , you can off it.
// redirection-------------------------------
return redirect('/home');
// you can redirect with parameter like:
return redirect('/posts',['id'=>2]); //or
return redirect('/posts')->with('id',34); // this data do not go with request , it saved inside session
return back(); // back to previous page
return back()->withInput(); // back sended data to previous again : but it not save in request or session you can use it in html with {{old('nameofdata')}} just for once
// response file! -------------------------
// you can return with response a file from your server!
$file =storage_path('app/public/test.txt');
return response()->download($file);
return response()->download($file,'custom_name');
// for show like pdf use :
$file =storage_path('app/public/data.pdf');
return response()->file($file);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment