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. โ๏ธ