Advertisement
heisenberg_

BookController (Client/Guzzle)

Feb 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.97 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use GuzzleHttp\Client as GuzzleHttpClient;
  7. use GuzzleHttp\Exception\RequestException;
  8.  
  9. class BookController extends Controller
  10. {
  11.    public function indexBook()
  12.    {
  13.         $client = New GuzzleHttpClient();
  14.         $apiRequest = $client->request('GET','http://localhost:8080/api/book');
  15.  
  16.         $books = json_decode($apiRequest->getBody()->getContents());
  17.  
  18.          // Mendapatkan data author utk input select
  19.           $apiAuthor = $client->request('GET','http://localhost:8080/api/author');
  20.           $contentAuthor = json_decode($apiAuthor->getBody()->getContents());
  21.            
  22.            // dd($contentAuthor);
  23.  
  24.         return view('book.index',['books' => $books,'contentAuthor' => $contentAuthor]);
  25.    }
  26.  
  27.    public function createBook()
  28.    {
  29.        // Mendapatkan data author utk input select
  30.       $client = New GuzzleHttpClient();
  31.       $apiAuthor = $client->request('GET','http://localhost:8080/api/author');
  32.       $contentAuthor = json_decode($apiAuthor->getBody()->getContents());
  33.  
  34.       return view('book.create', ['contentAuthor' => $contentAuthor]);
  35.    }
  36.  
  37.    public function storeBook(Request $request)
  38.     {
  39.       $client = New GuzzleHttpClient();
  40.  
  41.        $input = $request->all();
  42.        unset($input['_token']);
  43.  
  44.      
  45.  
  46.  
  47.        //Post to server
  48.        $apiRequest = $client->request('POST','http://localhost:8080/api/book',[
  49.         'form_params' => [
  50.  
  51.        'title' => $input['title'],
  52.       'author_id' => $input['author_id'],
  53.        'genres' => $input['genres'],
  54.        'synopsis' => $input['synopsis'],
  55.         ]
  56.         ]);
  57.  
  58.         return redirect()->route('book.index')->with('response', 'Add book successfully');
  59.    }
  60.  
  61.    public function editBook($id)
  62.    {
  63.      $client = New GuzzleHttpClient();
  64.      $response = $client->request('GET','http://localhost:8080/api/book/' . $id);
  65.  
  66.      $books = json_decode($response->getBody()->getContents());
  67.  
  68.       // Mendapatkan data author utk input select
  69.       $responseAuthor = $client->request('GET','http://localhost:8080/api/author');
  70.       $authors = json_decode($responseAuthor->getBody()->getContents());
  71.    
  72.      return view('book.edit',['books' => $books, 'authors'=> $authors]);
  73.  
  74.    }
  75.  
  76.    public function updateBook(Request $request, $id)
  77.    {
  78.  
  79.     $client = New GuzzleHttpClient();
  80.     $input = $request->all();
  81.        
  82.        $apiRequest = $client->request('PUT','http://localhost:8080/api/book/'. $id ,[
  83.         'form_params' => [
  84.  
  85.        'title' => $input['title'],
  86.       'author_id' => $input['author_id'],
  87.        'genres' => $input['genres'],
  88.        'synopsis' => $input['synopsis'],
  89.         ]
  90.         ]);
  91.  
  92.       return redirect()->route('book.index')->with('response', 'Update book successfully');
  93.  
  94.    }
  95.  
  96.    public function deleteBook($id)
  97.    {
  98.     $client = New GuzzleHttpClient();
  99.        $apiRequest = $client->request('DELETE','http://localhost:8080/api/book/'. $id);
  100.  
  101.        return redirect()->route('book.index')->with('response', 'Delete book successfully');
  102.  
  103.    }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement