complete step-by-step solution code

🟢 Step 1: Project Setup

mkdir composer-exam
cd composer-exam
composer init -y

Install packages:

composer require monolog/monolog
composer require guzzlehttp/guzzle
composer require phpmailer/phpmailer

🟢 Step 2: Folder Structure

composer-exam/

├── composer.json
├── vendor/
├── index.php
├── logger.php
├── api.php
├── mail.php
├── src/
│ └── User.php
├── logs/
│ └── app.log

🟡 Step 3: Logger System (logger.php)

<?php require 'vendor/autoload.php';use Monolog\Logger;
use Monolog\Handler\StreamHandler;// Create log channel
$log = new Logger('app');// Create logs directory if not exists
if (!file_exists('logs')) {
    mkdir('logs', 0777, true);
}// Add handler
$log->pushHandler(new StreamHandler('logs/app.log', Logger::DEBUG));// Logs
$log->info('User visited site');
$log->warning('Invalid login attempt');
$log->error('System failure');echo "Logs created successfully!";

🟡 Step 4: API Integration (api.php)

<?php require 'vendor/autoload.php';use GuzzleHttp\Client;$client = new Client();try {
    $response = $client->request('GET', 'https://jsonplaceholder.typicode.com/users');    $data = json_decode($response->getBody(), true);    echo "<h3>First 3 Users:</h3>";    for ($i = 0; $i < 3; $i++) {
        echo "Name: " . $data[$i]['name'] . "<br>";
        echo "Email: " . $data[$i]['email'] . "<br><br>";
    }} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

🟠 Step 5: Email System (mail.php)

<?php require 'vendor/autoload.php';use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;$mail = new PHPMailer(true);try {
    // SMTP Settings (example: Gmail)
    $mail->isSMTP();
    $mail->Host       = 'smtp.gmail.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'your_email@gmail.com';
    $mail->Password   = 'your_app_password'; // Use App Password
    $mail->SMTPSecure = 'tls';
    $mail->Port       = 587;    // Email settings
    $mail->setFrom('your_email@gmail.com', 'Test');
    $mail->addAddress('test@example.com');    $mail->Subject = 'Test Mail';
    $mail->Body    = 'Hello from Composer';    $mail->send();
    echo "Email sent successfully!";} catch (Exception $e) {
    echo "Mailer Error: {$mail->ErrorInfo}";
}

🔴 Step 6: PSR-4 Autoloading

Update composer.json

{
"require": {
"monolog/monolog": "^3.0",
"guzzlehttp/guzzle": "^7.0",
"phpmailer/phpmailer": "^6.0"
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}

Run:

composer dump-autoload

Create Class: src/User.php

<?php namespace App;class User {
    public function getName() {
        return "Aditya";
    }
}

Use in index.php

<?php require 'vendor/autoload.php';use App\User;$user = new User();echo $user->getName();

🔴 Step 7: Mini Project (Contact Form)

👉 form.html

<!DOCTYPE html>
<html>
<head>
<title>Contact Form</title>
</head>
<body><form method="POST" action="contact.php">
Name: <input type="text" name="name" required><br><br>
Email: <input type="email" name="email" required><br><br>
Message: <textarea name="message" required></textarea><br><br>
<button type="submit">Send</button>
</form></body>
</html>

👉 contact.php

<?php require 'vendor/autoload.php';use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use PHPMailer\PHPMailer\PHPMailer;// Get form data
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];// Validate
if (empty($name) || empty($email) || empty($message)) {
    die("All fields are required!");
}// Logger
$log = new Logger('contact');
$log->pushHandler(new StreamHandler('logs/app.log', Logger::INFO));
$log->info("Contact form submitted by: $email");// Send Email
$mail = new PHPMailer(true);try {
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'your_email@gmail.com';
    $mail->Password = 'your_app_password';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;    $mail->setFrom($email, $name);
    $mail->addAddress('your_email@gmail.com');    $mail->Subject = 'New Contact Message';
    $mail->Body = "Name: $name\nEmail: $email\nMessage: $message";    $mail->send();    echo "Message sent successfully!";} catch (Exception $e) {
    echo "Error: {$mail->ErrorInfo}";
}

🧠 Step 8: Restore Project (Important)

👉 If vendor/ deleted:

composer install

✅ Final Output Checklist

✔ Logs created
✔ API data fetched
✔ Email working
✔ Autoload working
✔ Contact form working


🚀 Pro Tips (Real Industry)

  • Use .env for email credentials
  • Never hardcode passwords
  • Always validate user input
  • Use MVC structure for large apps