Docs available in pt-BR
- PHP installed on your machine. 🐘
- Composer installed on your machine. 📦
- Configured MySQL database. 🗄️
- Basic understanding of PHP programming. 🧠
-
Clone the project or download the template to your local directory. 📂
-
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.
-
This will install all dependencies listed in the
composer.json
file and ensure that you work with the exact versions specified incomposer.lock
. 🔒
-
Locate the .env.example file in the project directory. 🗂️
-
Rename the file to
.env
:Command prompt:
rename .env.example .env
PowerShell:
mv .env.example .env
-
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. 🏷️
-
-
Save changes to the
.env
file. 💾
-
Create a DAO class for each database entity (e.g.
UserDAO
for theusers
table). 👥 -
Implement methods for specific operations, such as
selectAll()
. 💼 -
Use the database connection to run SQL queries. 💻
<?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);
}
}
-
Create a Model class for each database entity (e.g.
UserModel
). 👤 -
Define attributes to represent the database table columns. 📊
-
Implement methods to interact with the DAO, such as
getAll()
. 🛠️
<?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;
}
}
}
-
Create a Controller class for each feature or set of functionality in your API (e.g.
UserController
). 💡 -
In the Controller, create methods to respond to specific HTTP requests, such as
GET
,POST
,PUT
, andDELETE
. 📝 -
Call Model methods to process data received from requests and send appropriate responses to the client. 📨
<?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);
}
}
- 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"]);
-
To start the application, run the following command in the project root directory:
php api start
-
This will start the server at the address and port defined in
.env
.
-
Make HTTP requests to your API endpoints to verify that everything is working as expected. ✅
-
Use tools like Insomnia to send
GET
,POST
,PUT
, andDELETE
requests. 📮 -
Check response to ensure data is processed correctly. ✔️