SkillAgentSearch skills...

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/MVC
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

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

  1. Clone the repository
git clone https://github.com/zakaria-123kardache/MVC.git
cd MVC
  1. Install dependencies
composer install
  1. Configure environment
cp .env.example .env
  1. 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

  1. Fork the repository
  2. Create your feature branch ( git checkout -b feature/AmazingFeature)
  3. Commit your changes ( git commit -m 'Add some AmazingFeature')
  4. Push to the branch ( git push origin feature/AmazingFeature)
  5. Open a Pull Request

🤝 ARtikles

Related Skills

View on GitHub
GitHub Stars6
CategoryDevelopment
Updated1y ago
Forks0

Languages

PHP

Security Score

55/100

Audited on Mar 9, 2025

No findings