MVC
This project aims to design a clean and modular MVC architecture in PHP, using PostgreSQL as the database. The goal is to have a strict separation of responsibilities, a Back Office for administration, and a Front Office for public display. The architecture should be extensible, secure, and well-structured, applying best development practices
Install / Use
/learn @kardasch404/MVCREADME
PHP MVC Framework
A lightweight MVC framework built with PHP and PostgreSQL, featuring a clean architecture and modular design.
Features
- MVC Architecture
- PostgreSQL Integration
- Environment Configuration
- Component-based Views
- Clean Routing System
- PDO Database Layer
Project Structure
project/
├── app/
│ ├── Controllers/
│ | └── Controller.php/
│ | └── IndexController.php/
│ └── Config/
│ | └── Database.php/
│ └── Models/
│ | └── Home.php/
│ └── Repositories/
│ | └── Repository.php/
│ └── Routes/
│ | └── Route.php/
│ | └── Router.php/
├── public/
│ ├── assets/
│ | └── css/
│ | | └── app.css
│ | └── js/
│ | | └── app.js
│ └── index.php
├── resources/
│ ├── views/
│ | ├── components/
│ | | ├── footer.php/
│ | | ├── navbar.php/
│ | | ├── header.php/
│ | ├── pages/
│ | | ├── home.php/
│ | ├── 404.php/
├── .env/
└── README.md
Installation
- Clone the repository
git clone https://github.com/zakaria-123kardache/MVC.git
cd MVC
- Install dependencies
composer install
- Configure environment
cp .env.example .env
- Update .env with your database credentials
DB_HOST=localhost
DB_USERNAME=your_username
DB_PASSWORD=your_password
DB_DATABASE=your_database
DB_PORT=5432
Core Components
Database Config (Database.php)
<?php
namespace App\Config;
use Dotenv\Dotenv;
class Database
{
private static $instance = null;
private $pdo;
private function __construct($host, $user, $password, $dbname, $port)
{
$dsn = 'pgsql:host=' . $host .';port='.$port. ';dbname=' . $dbname;
$options = array(
\PDO::ATTR_PERSISTENT => true,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
);
try {
$this->pdo = new \PDO($dsn, $user, $password, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int) $e->getCode());
}
}
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = self::createInstanceFromEnv();
}
return self::$instance;
}
private static function createInstanceFromEnv()
{
$dotenv = Dotenv::createUnsafeImmutable(__DIR__ . '/../../');
$dotenv->load();
$host = $_ENV['DB_HOST'];
$user = $_ENV['DB_USERNAME'];
$password = $_ENV['DB_PASSWORD'];
$dbname = $_ENV['DB_DATABASE'];
$port = $_ENV['DB_PORT']??'5432';
return new self($host, $user, $password, $dbname,$port);
}
public function getPdo()
{
return $this->pdo;
}
}
Base Controller (Controller.php)
<?php
namespace App\Controller;
class Controller {
protected $viewPath;
protected $model;
public function __construct()
{
$this->viewPath = __DIR__."/../../resources/views";
$modelName = str_replace('Controller', '',static::class);
$modelName = strtolower($modelName);
}
protected function render($view, $data = [])
{
$file = __DIR__ . '/../../resources/views/' . str_replace('.', '/', $view) . '.php';
if (file_exists($file)) {
extract($data);
include $file;
}
}
}
Index Controller (IndexController.php)
<?php
namespace App\Controller;
use App\Controller\Controller;
class IndexController extends Controller {
public function index()
{
return $this->render("pages.home");
}
public function about()
{
return $this->render("pages.about");
}
}
🔧 Usage Examples
Creating a New Controller
namespace App\Controller;
class UserController extends Controller {
public function index() {
return $this->render("pages.users.index", [
'users' => $users
]);
}
}
🤝 Contributing
- Fork the repository
- Create your feature branch ( git checkout -b feature/AmazingFeature)
- Commit your changes ( git commit -m 'Add some AmazingFeature')
- Push to the branch ( git push origin feature/AmazingFeature)
- Open a Pull Request
🤝 ARtikles
- Meduim - MVC Fremwork
Related Skills
node-connect
346.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
346.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
346.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
