LovisCoding / SAE401-back

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Docs available in pt-BR

PHP API Starter Template ๐Ÿ’ป

Prerequisites ๐Ÿ› ๏ธ

  • PHP installed on your machine. ๐Ÿ˜
  • Composer installed on your machine. ๐Ÿ“ฆ
  • Configured MySQL database. ๐Ÿ—„๏ธ
  • Basic understanding of PHP programming. ๐Ÿง 

Dependency Installation ๐Ÿš€

  1. Clone the project or download the template to your local directory. ๐Ÿ“‚

  2. Navigate to your project directory and run the following command to install the dependencies:

    composer install

    If you don't have Composer installed, you can download it from the official website.

  3. This will install all dependencies listed in the composer.json file and ensure that you work with the exact versions specified in composer.lock. ๐Ÿ”’

Environment Variables Configuration โš™๏ธ

  1. Locate the .env.example file in the project directory. ๐Ÿ—‚๏ธ

  2. Rename the file to .env:

    Command prompt:

    rename .env.example .env

    PowerShell:

    mv .env.example .env
  3. Open the .env file in a text editor and configure the environment variables as needed: ๐Ÿ“

    • API Configuration:

      • API_HOST: The address where the API will be executed (e.g. localhost). ๐ŸŒ
      • API_PORT: The port on which the API will run (e.g. 8000). ๐Ÿ”Œ
      • TIMEZONE: The application's time zone (for example, Europe/Paris). ๐Ÿ•ฐ๏ธ
    • Database Configuration:

      • DB_HOST: The address of the database server. ๐Ÿ 
      • DB_PORT: The database server port. ๐Ÿ”Œ
      • DB_USER: The username to access the database. ๐Ÿ‘ค
      • DB_PASS: The password to access the database. ๐Ÿ”‘
      • DB_NAME: The name of the database. ๐Ÿท๏ธ
  4. Save changes to the .env file. ๐Ÿ’พ

Configuring DAO, Model and Controller ๐Ÿ› ๏ธ

DAO (Data Access Object) ๐Ÿ—ƒ๏ธ

  1. Create a DAO class for each database entity (e.g. UserDAO for the users table). ๐Ÿ‘ฅ

  2. Implement methods for specific operations, such as selectAll(). ๐Ÿ’ผ

  3. Use the database connection to run SQL queries. ๐Ÿ’ป

Here is an example of UserDAO:

<?php

namespace App\DAO;

class UserDAO extends DAO
{
    // Constructor to initialize the UserDAO object
    public function __construct()
    {
        // Call the parent class constructor (DAO) to initialize the database connection
        parent::__construct();
    }

    // Method to select all records from the "User" table
    public function selectAll()
    {
        // Definition of the SQL query to select all records from the "User" table
        $sql = "SELECT * FROM User";

        // Prepare SQL query using database connection
        $stmt = $this->conn->prepare($sql);

        // Execute the prepared query
        $stmt->execute();

        // Returns all query results as class objects (based on DAO class)
        return $stmt->fetchAll(DAO::FETCH_CLASS);
    }
}

Model

  1. Create a Model class for each database entity (e.g. UserModel). ๐Ÿ‘ค

  2. Define attributes to represent the database table columns. ๐Ÿ“Š

  3. Implement methods to interact with the DAO, such as getAll(). ๐Ÿ› ๏ธ

Here is an example UserModel:

<?php

namespace App\Model;

use App\DAO\UserDAO;
use Exception;

class UserModel extends Model
{
    // Public attributes to represent the columns of the User table
    public $id, $name;

    // Method to get all records from the User table
    public function getAll()
    {
        try {
            // Create a UserDAO instance to access the data layer
            $dao = new UserDAO();
            // Call UserDAO selectAll() method to get all records
            // Results are stored in the $rows property
            $this->rows = $dao->selectAll();
        } catch (Exception $e) {
            // Throws an exception in case of error during execution
            throw $e;
        }
    }
}

Controller โš™๏ธ

  1. Create a Controller class for each feature or set of functionality in your API (e.g. UserController). ๐Ÿ’ก

  2. In the Controller, create methods to respond to specific HTTP requests, such as GET, POST, PUT, and DELETE. ๐Ÿ“

  3. Call Model methods to process data received from requests and send appropriate responses to the client. ๐Ÿ“จ

Here is an example of a UserController:

<?php

namespace App\Controller;

use App\Model\UserModel;

class UserController extends Controller
{
    // Static method that will be executed when the corresponding route is accessed
    public static function index(): void
    {
        // Create a new UserModel instance to access the model layer
        $model = new UserModel;
        
        // Call UserModel getAll() method to get all records
        $model->getAll();
        
        // Sends the response in JSON format containing the records obtained
        parent::sendJSONResponse($model->rows);
    }
}

Registering api routes โ†ช๏ธ

  1. Add the necessary routes in the routes.php file:
<?php
use App\Controller\UserController;
use App\Modules\HttpMethod;
use App\Modules\Router;

Router::request(HttpMethod::GET, "/users", [UserController::class, "index"]);

Starting the Application ๐Ÿ

  1. To start the application, run the following command in the project root directory:

    php api start
  2. This will start the server at the address and port defined in .env.

Testing the API

  1. Make HTTP requests to your API endpoints to verify that everything is working as expected. โœ…

  2. Use tools like Insomnia to send GET, POST, PUT, and DELETE requests. ๐Ÿ“ฎ

  3. Check response to ensure data is processed correctly. โœ”๏ธ

About

License:MIT License


Languages

Language:PHP 98.4%Language:PLpgSQL 1.6%