Angular – How to manage my http requests in PHP

  angular, http, httpclient, php

So im a rookie at Angular and im having some ussues related to how should i organize my http requests.
At the moment i am making some request to an "API" i made in PHP, from a service file (item.service.ts) :

import { Injectable } from '@angular/core';
import { Item } from  '../_interfaces/item';

  providedIn: 'root'
export class ItemService {
  //php server ip
  private PHP_API_SERVER = "";
  private params= {
    responseType: 'json' as const,

  constructor(private httpClient: HttpClient) {}

  get_all(): Observable<Item[]>{
    return this.httpClient.get<Item[]>(`${this.PHP_API_SERVER}/api/item.php`,{ params: this.params});

And in item.php i wanted to hundle all item related requests, which i cant atm…

require '_database.php';

$items = [];
$sql = "SELECT * FROM items";

if($result = mysqli_query($con,$sql))
  $i = 0;
  while($row = mysqli_fetch_assoc($result))
    $items[$i]['id'] = $row['id'];
    $items[$i]['field_1'] = $row['field_1'];
    $items[$i]['field_2'] = $row['field_2'];
  echo json_encode($items);

i was thinking in using the http params, but im not sure whats the best approach.

add_item(item: Item): Observable<Item>{
    return<Item>(`${this.PHP_API_SERVER}/api/item.php`, item);

Source: Angular Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.