Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use GuzzleHttp\Exception\GuzzleException;
- use GuzzleHttp\Client;
- class BookController extends Controller
- {
- protected $url;
- protected $client;
- function __construct()
- {
- $this->url = "http://localhost:8000/";
- $this->client = new Client;
- }
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- $request = $this->client->request('GET', $this->url.'api/book');
- $data['books' ] = json_decode($request->getBody()->getContents());
- return view('book.index',$data);
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- return view('book.create');
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- $request = $this->client->request('POST',$this->url.'api/book', [
- 'form_params' => [
- 'title' => $request->title,
- 'price' => $request->price
- ]
- ]);
- $result = json_decode($request->getBody()->getContents());
- return redirect('book')->with('message','A New Book With Title '.$result->data->title.' Has Created');
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- //
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit($id)
- {
- $request = $this->client->request('GET', $this->url.'api/book/'.$id);
- $data['book' ] = json_decode($request->getBody()->getContents());
- return view('book.edit',$data);
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- $request = $this->client->request('PUT',$this->url.'api/book/'.$id, [
- 'form_params' => [
- 'title' => $request->title,
- 'price' => $request->price
- ]
- ]);
- $result = json_decode($request->getBody()->getContents());
- return redirect('book')->with('message','A New Book With Title '.$result->data->title.' Has Updated');
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- //
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement