src/Controller/DolarController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Contracts\HttpClient\HttpClientInterface;
  7. class DolarController extends AbstractController
  8. {
  9.     private HttpClientInterface $client;
  10.     public function __construct(HttpClientInterface $client)
  11.     {
  12.         $this->client $client;
  13.     }
  14.      #[Route('/api/dolar'name'api_dolar')]
  15.     public function api(): Response
  16.     {
  17.         try{
  18.             $oficial $this->client->request('GET''https://dolarapi.com/v1/dolares/oficial')->toArray();
  19.             $blue $this->client->request('GET''https://dolarapi.com/v1/dolares/blue')->toArray();
  20.         } catch (\Exception $e) {
  21.             $oficial = ['compra' => 'N/D''venta' => 'N/D'];
  22.             $blue = ['compra' => 'N/D''venta' => 'N/D'];
  23.         }
  24.         
  25.         return $this->render('_includes/_headerDolar.html.twig', [
  26.             'oficial' => $oficial,
  27.             'blue' => $blue,
  28.         ]);
  29.     }
  30. }