Skip to content

PhyoZ13/php-mvc-laravel-style

Repository files navigation

Simple PHP MVC Category & Product CRUD with Authentication

This is a simple PHP MVC (Model-View-Controller) project for Category and Product CRUD (Create, Read, Update, Delete) operations, using OOP and MySQL, without any frameworks. Includes user authentication, Eloquent ORM, and .env support.


📁 Project Structure

/core-php/
│
├── app/
│   ├── Controllers/    # Controllers (CategoryController.php, ProductController.php, AuthController.php)
│   ├── Models/         # Models (Category.php, Product.php, User.php)
│   └── Core/           # Core classes (App.php, Router.php, Eloquent.php)
├── views/
│   └── category/       # Views for category CRUD (index.php, create.php, edit.php)
│   └── product/        # Views for product CRUD (index.php, create.php, edit.php)
│   └── auth/           # Views for authentication (login.php, register.php)
├── public/             # Entry point (index.php)
├── routes/
│   └── web.php         # Route definitions
├── .env                # Environment variables (DB config, etc.)
├── composer.json       # Composer dependencies and autoload
└── vendor/             # Composer packages

🚀 Step-by-Step Setup

1. Clone or Download

git clone <repo-url> core-php
cd core-php

2. Install Dependencies

composer install

3. Database Setup

  1. Create the database:
    CREATE DATABASE `core-php`;
  2. Create the categories table:
    CREATE TABLE categories (
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(255) NOT NULL,
        status TINYINT(1) NOT NULL DEFAULT 1,
        is_deleted TINYINT(1) NOT NULL DEFAULT 0,
        sku_code VARCHAR(100) NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
  3. Create the products table:
    CREATE TABLE products (
        id INT AUTO_INCREMENT PRIMARY KEY,
        category_id INT NOT NULL,
        name VARCHAR(255) NOT NULL,
        price DECIMAL(10,2) NOT NULL,
        status TINYINT(1) NOT NULL DEFAULT 1,
        is_deleted TINYINT(1) NOT NULL DEFAULT 0,
        sku_code VARCHAR(100) NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        FOREIGN KEY (category_id) REFERENCES categories(id)
    );
  4. Create the users table:
    CREATE TABLE users (
        id INT AUTO_INCREMENT PRIMARY KEY,
        username VARCHAR(50) NOT NULL UNIQUE,
        password VARCHAR(255) NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );

4. Configure Environment (.env)

Create a .env file in your project root:

APP_NAME=CorePHP
APP_ENV=local
APP_DEBUG=true

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=core-php
DB_USERNAME=root
DB_PASSWORD=adminpass

5. Run on Localhost

Using PHP Built-in Server (Recommended for Testing)

  1. Open terminal in the project root (core-php)
  2. Run:
    php -S localhost:8000 -t public
  3. Open your browser and go to: http://localhost:8000

Using Apache/Nginx

  • Set your DocumentRoot to the public/ folder.
  • Example for Apache:
    DocumentRoot /path/to/core-php/public
    

📝 Usage

  • Authentication:
    • /login — Login page
    • /register — Register page
    • /logout — Logout
  • List Categories: /category
  • Add Category: /category/create
  • Edit Category: /category/edit?id=1
  • Delete Category: /category/delete?id=1
  • List Products: /product
  • Add Product: /product/create
  • Edit Product: /product/edit?id=1
  • Delete Product: /product/delete?id=1

🛠️ Composer, PSR-4 & Eloquent ORM

  • All classes are autoloaded via Composer and PSR-4. No manual require_once needed.
  • All models extend Illuminate\Database\Eloquent\Model and use Eloquent ORM for database access.
  • All views use Eloquent object properties (e.g., $category->name, $product->category->name).
  • To regenerate the autoloader after adding new classes:
    composer dump-autoload
  • To add new dependencies:
    composer require <package-name>

🧠 How It Works

  • Models: Eloquent ORM, no legacy methods or properties.
  • Controllers: Use Eloquent for all DB operations.
  • Views: Use Eloquent object properties.
  • Config: All sensitive config is in .env (never commit this file to public repos).
  • Routing: Centralized in routes/web.php.
  • Entry Point: public/index.php bootstraps Composer and the app.

📚 Learn & Extend

  • Follow the code flow from public/index.php.
  • Try adding new fields or new entities (e.g., orders).
  • Add validation, relationships, or more features as you learn.
  • You can now use Eloquent relationships, scopes, and more advanced features.

💬 Need Help?

Open an issue or ask for guidance!

About

Native PHP, MVC, Composer Eloquent, Laravel Style

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages