This project is a lightweight, modern MVC template built with PHP and Composer, following the Model-View-Controller (MVC) architecture. It provides a clean, organized structure for developing PHP applications, with features like user authentication, image uploading, email handling, environment configuration, and styling with Tailwind CSS via the Play CDN.
This README guides you through setting up the project, using its features, and understanding its structure.
- Prerequisites
- Installation
- Project Structure
- Configuration
- Usage
- Environment Configuration
- Troubleshooting
- Contributing
- License
Before you begin, ensure you have the following installed:
- PHP >= 7.4
- Composer: Download Composer
- Database: MySQL or PostgreSQL (configured in
.env
) - Internet Connection: Required for loading Tailwind CSS via the Play CDN and jQuery CDN.
- Clone the repository:
git clone https://github.com/lizzyman04/composer-mvc-template.git my-composer-app
- Navigate to the project directory:
cd my-composer-app
- Install the required Composer dependencies:
composer install
- Set up your environment file by copying the default
.env.example
file to.env
:
cp .env.example .env
-
Update your
.env
file with your database and email configurations. -
Set up your database by running migrations or manually creating tables according to your project's needs.
-
Composer provides useful scripts for managing the project:
- Start the local development server on
http://localhost:8000
:
composer serve
- Run migrations to create missing database tables:
composer migrate
- Reset the database (drops all tables and recreates them):
composer migrate:reset
Note: The
migrate:reset
command deletes all existing tables and recreates them. Themigrate
command only creates tables that do not yet exist. - Start the local development server on
-
Start the local server:
composer serve
Visit http://localhost:8000 in your browser.
Here's an overview of the main project directories and files:
/composer-mvc-template
├── /public
│ ├── /assets
│ │ ├── /css
│ │ ├── /img
│ │ └── /js
│ ├── index.php
│ ├── 404.php
│ ├── /uploads
├── /src
│ ├── /Controllers
│ ├── /Models
│ ├── /Views
│ │ ├── /layouts
│ │ │ ├── main.php
│ │ │ ├── tailwinds.php
│ ├── └──...
├── /core
│ ├── Mailer.php
│ ├── Router.php
│ ├── Uploader.php
│ └── View.php
├── /database
│ ├── connection.php
│ └── migrate.php
├── /config
│ ├── config.php
│ ├── database.php
│ └── endpoints.php
├── .env
├── .env.example
├── .gitignore
├── composer.json
├── LICENSE
└── README.md
-
/public: Contains publicly accessible files:
/assets
: Static files (eg: images, JavaScript).index.php
: Application entry point./uploads
: Directory for user-uploaded files (e.g., images).
-
/src: Core application logic:
/Controllers
: Handles requests and interacts with models./Models
: Defines data structures and interacts with the database./Views
: Templates for rendering HTML pages./Views/layouts
: Contains layout files includingmain.php
(base template).
-
/core: Utility classes:
Mailer.php
: Manages email sending.Router.php
: Handles routing (uses/config/endpoints.php
).Uploader.php
: Manages file uploads.View.php
: Renders views and templates.
-
/database: Database management files:
connection.php
: Establishes database connections.migrate.php
: Manages database migrations (uses/config/database.php
).
-
/config: Configuration files:
config.php
: Main project configuration.database.php
: Database settings and migration functions.endpoints.php
: Application routes.
-
.env: Stores sensitive configuration (e.g., database credentials, email settings).
-
.env.example: Template for creating
.env
. -
.gitignore: Specifies files/directories ignored by Git.
-
composer.json: Manages Composer dependencies.
-
LICENSE: Project license.
-
README.md: Project documentation.
Update the .env
file with your database credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password
Configure SMTP settings in the .env
file for email functionality:
MAIL_HOST=smtp.your-email-provider.com
MAIL_PORT=587
MAIL_USERNAME=[email protected]
MAIL_PASSWORD=your-email-password
MAIL_FROM_ADDRESS=[email protected]
MAIL_FROM_NAME=YourApp
SUPPORT_EMAIL=[email protected]
The project includes user authentication with login and registration forms, handled by the AuthController
. Customize the views in /src/Views/auth.php
as needed.
- Login: Users provide email and password to log in.
- Registration: Users create accounts with name, email, and password.
The AuthController
validates input and manages authentication.
The Uploader
class handles file uploads (e.g., images):
use Core\Uploader;
if ($_FILES['image']) {
try {
$targetDir = 'uploads/images';
$uploadedImagePath = Uploader::uploadImage($_FILES['image'], $targetDir);
echo "Image uploaded to: " . $uploadedImagePath;
} catch (\Exception $e) {
echo "Error: " . $e->getMessage();
}
}
Ensure the uploads/
directory has write permissions (e.g., chmod 755 uploads
).
The Mailer
class uses PHPMailer to send emails:
use Core\Mailer;
$mailer = new Mailer();
$name = "Arlindo Abdul";
$message = "Welcome to our platform! We're glad to have you on board.";
$body = $mailer->create($name, $message);
$sendResult = $mailer->send('[email protected]', $name, 'Welcome to Our Platform', $body);
if ($sendResult['success']) {
echo "Email sent successfully!";
} else {
echo "Failed to send email: " . $sendResult['message'];
}
Emails are sent in HTML format with personalized greetings and a footer.
This project uses Tailwind CSS via the Play CDN for styling, eliminating the need for a build step during development. The base layout (/src/Views/layouts/main.php
) includes the Tailwind CSS CDN.
Apply Tailwind utility classes directly in your views:
<div class="bg-gray-100 p-6 rounded-lg shadow-md">
<h1 class="text-3xl font-bold text-clifford">Welcome!</h1>
<p class="mt-4 text-gray-600">This is a Tailwind-styled component.</p>
</div>
The project includes jQuery via a CDN for client-side scripting, loaded in the main.php
layout. Use jQuery to add interactivity to your views.
<script>
$(document).ready(function() {
$('.toggle-button').click(function() {
$('.content').toggleClass('hidden');
});
});
</script>
<button class="toggle-button bg-blue-500 text-white px-4 py-2 rounded">Toggle</button>
<div class="content mt-4">This content can be toggled.</div>
Ensure jQuery scripts are placed after the CDN include in main.php
.
-
Missing Dependencies: If
composer install
fails, ensure PHP extensions (e.g.,pdo_mysql
) are enabled. Re-runcomposer install
. -
Database Connection Error: Verify
.env
database credentials and ensure the database server is running. -
Email Sending Issues: Check
.env
SMTP settings. Test with a third-party email service if needed. -
File Upload Issues: Ensure the
uploads/
directory has write permissions (e.g.,chmod 755 uploads
). -
Tailwind CSS Not Loading: Confirm an active internet connection for the Play CDN. For production, switch to a static build.
-
jQuery Errors: Ensure jQuery scripts are loaded after the CDN include. Check browser console for errors.
Contributions are welcome! To contribute:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/your-feature
). - Commit your changes (
git commit -m 'Add your feature'
). - Push to the branch (
git push origin feature/your-feature
). - Open a pull request.
Please include tests and documentation for your changes.
This project is licensed under the MIT License. See the LICENSE file for details.
This Composer MVC template provides a robust foundation for PHP applications, with features like authentication, file uploads, email handling, Tailwind CSS for styling, and jQuery for interactivity. With minimal setup, you can build and customize powerful web applications.
Happy coding! 🚀