Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use GuzzleHttp\Exception\GuzzleException;
  7. use GuzzleHttp\Client;
  8.  
  9.  
  10. class BookController extends Controller
  11. {
  12.  
  13. protected $url;
  14. protected $client;
  15.  
  16. function __construct()
  17. {
  18. $this->url = "http://localhost:8000/";
  19. $this->client = new Client;
  20. }
  21. /**
  22. * Display a listing of the resource.
  23. *
  24. * @return \Illuminate\Http\Response
  25. */
  26. public function index()
  27. {
  28. $request = $this->client->request('GET', $this->url.'api/book');
  29. $data['books' ] = json_decode($request->getBody()->getContents());
  30. return view('book.index',$data);
  31. }
  32.  
  33. /**
  34. * Show the form for creating a new resource.
  35. *
  36. * @return \Illuminate\Http\Response
  37. */
  38. public function create()
  39. {
  40. return view('book.create');
  41. }
  42.  
  43. /**
  44. * Store a newly created resource in storage.
  45. *
  46. * @param \Illuminate\Http\Request $request
  47. * @return \Illuminate\Http\Response
  48. */
  49. public function store(Request $request)
  50. {
  51. $request = $this->client->request('POST',$this->url.'api/book', [
  52. 'form_params' => [
  53. 'title' => $request->title,
  54. 'price' => $request->price
  55. ]
  56. ]);
  57.  
  58. $result = json_decode($request->getBody()->getContents());
  59. return redirect('book')->with('message','A New Book With Title '.$result->data->title.' Has Created');
  60. }
  61.  
  62. /**
  63. * Display the specified resource.
  64. *
  65. * @param int $id
  66. * @return \Illuminate\Http\Response
  67. */
  68. public function show($id)
  69. {
  70. //
  71. }
  72.  
  73. /**
  74. * Show the form for editing the specified resource.
  75. *
  76. * @param int $id
  77. * @return \Illuminate\Http\Response
  78. */
  79. public function edit($id)
  80. {
  81. $request = $this->client->request('GET', $this->url.'api/book/'.$id);
  82. $data['book' ] = json_decode($request->getBody()->getContents());
  83. return view('book.edit',$data);
  84. }
  85.  
  86. /**
  87. * Update the specified resource in storage.
  88. *
  89. * @param \Illuminate\Http\Request $request
  90. * @param int $id
  91. * @return \Illuminate\Http\Response
  92. */
  93. public function update(Request $request, $id)
  94. {
  95. $request = $this->client->request('PUT',$this->url.'api/book/'.$id, [
  96. 'form_params' => [
  97. 'title' => $request->title,
  98. 'price' => $request->price
  99. ]
  100. ]);
  101.  
  102. $result = json_decode($request->getBody()->getContents());
  103. return redirect('book')->with('message','A New Book With Title '.$result->data->title.' Has Updated');
  104. }
  105.  
  106. /**
  107. * Remove the specified resource from storage.
  108. *
  109. * @param int $id
  110. * @return \Illuminate\Http\Response
  111. */
  112. public function destroy($id)
  113. {
  114. //
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement