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.
/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
git clone <repo-url> core-php
cd core-php
composer install
- Create the database:
CREATE DATABASE `core-php`;
- 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 );
- 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) );
- 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 );
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
- Open terminal in the project root (
core-php
) - Run:
php -S localhost:8000 -t public
- Open your browser and go to: http://localhost:8000
- Set your DocumentRoot to the
public/
folder. - Example for Apache:
DocumentRoot /path/to/core-php/public
- 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
- 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>
- 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.
- 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.
Open an issue or ask for guidance!